Ios 如何在目标C中的类之间传递值?
我目前一直在想为什么我的值没有在xcode中传递。我的代码看起来是正确的,但由于某种原因它无法通过 这是视图。h看起来像什么Ios 如何在目标C中的类之间传递值?,ios,objective-c,xcode,Ios,Objective C,Xcode,我目前一直在想为什么我的值没有在xcode中传递。我的代码看起来是正确的,但由于某种原因它无法通过 这是视图。h看起来像什么 //View Header File #import <UIKit/UIKit.h> IB_DESIGNABLE @interface TheView : UIView @property NSString *dateTimeString; @end 在我的controller.h文件中,我有以下代码 // Controller File #import
//View Header File
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface TheView : UIView
@property NSString *dateTimeString;
@end
在我的controller.h文件中,我有以下代码
// Controller File
#import "TheView.h"
@interface MainViewController: UIViewController
@property (weak, nonatomic) IBOutlet TheView *rootView;
@end
MainViewController.m文件
#import "MainViewController.h"
#import "TheView.h"
@interface MainViewController ()
@property (string, nonatomic) NSTimer *currentTime;
@end
@implementation MainViewController
-(void)updateTime
{
// prints date successfully
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale time = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
dateFormatter.locale = time;
formatString = [NSDATEFORMATTER dateFormatFromTemplate:@"EEEE, hh:mm" options:0 locale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:formatString];
NSString *resultString = [dateFormatter stringFromDate:[NSDate date]];
// Current issue is that it isn't passed from here to TheView.m
[_rootView setdateTimeString:resultString];
}
根据我在调试中的理解,控制器中的字符串给出了我需要的结果,但它没有设置值
如果有帮助的话,这段代码将被称为viewwill,因此它将调用updateTime方法,理论上不会为零
DisplayView
是在TheView.h
中声明的DisplayView
应该在DisplayView.h
和DisplayView.m
\u rootview.dateTimeString=resultString代码>
resultString
嘿,我真的不明白你在1上的意思。你能给我解释一下吗?我更新了代码,因为我看到了一些不正确的东西。我将displayViewController更新为另一个名称,因为我不知道它是一个关键字。我的坏现在rootView的类是DisplayView,但是dateTimeString属于您的类“TheView”。这是一场班级比赛。我想我在编辑中修正了它。这似乎是对的还是我仍然缺少什么?如何将值分配给
\u rootView
?很可能是nil
设置程序将值分配给_rootView,获取结果字符串并将其传递给视图。我做得对吗?结果是我没有把它挂在故事板上,这就是它不起作用的原因
#import "MainViewController.h"
#import "TheView.h"
@interface MainViewController ()
@property (string, nonatomic) NSTimer *currentTime;
@end
@implementation MainViewController
-(void)updateTime
{
// prints date successfully
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale time = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
dateFormatter.locale = time;
formatString = [NSDATEFORMATTER dateFormatFromTemplate:@"EEEE, hh:mm" options:0 locale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:formatString];
NSString *resultString = [dateFormatter stringFromDate:[NSDate date]];
// Current issue is that it isn't passed from here to TheView.m
[_rootView setdateTimeString:resultString];
}