如何正确初始化实例化视图控制器中的控件?

时间:2016-12-05 10:11:30

标签: objective-c

我有一个班级ErrorMessageViewController

// ErrorMessageViewController.h
@interface ErrorMessageViewController: UIViewController {
    NSString * tempTitle;
    NSString * tempContent;
}
@property (nonatomic, strong) IBOutlet UILabel* messageTitle;
@property (nonatomic, strong) IBOutlet UILabel* messageContent;
-(void) initTitle:(NSString *)title andContent:(NSString *)content;
@end

// ErrorMessageViewController.m
@implementation ErrorMessage
@synthesize messageTitle, messageContent;

-(void) initTitle:(NSString *)title andContent:(NSString *)content {
    tempTitle = title == nil ? @"" : title;
    tempContent = content == nil ? @"" : content;
}

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (messageTitle != nil) { [messageTitle setText:tempTitle]; }
    if (messageContent != nil) { [messageContent setText:tempContent]; }
}

@end

然后我使用此代码将其显示出来:

NSString * errorMessage = @"Duplicate ID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ErrorMessageViewController * errorMessage = (ErrorMessageViewController *)
[storyboard instantiateViewControllerWithIdentifier:@"ErrorMessage"];
[errorMessage initTitle:@"Error" andContent:contentVar];
[[self navigationController] pushViewController:errorMessage animated:YES];

当我调试initTitle:Content:时,tempTitletempContentNSCFConstString"错误"和NSCFString"重复ID"。

然后流程转到viewWillAppeartempTitletempContent的内容都消失了,取而代之的是NSCFString和{{1} }。 tempTitle分配仍然正常,因为它仍然是字符串,但我不知道内容是什么,但tempContent分配产生错误。我甚至无法记录NSObject值。

为什么会这样?为什么对象的内容会发生变化,就像它从未初始化一样?

2 个答案:

答案 0 :(得分:1)

故事板使用延迟加载来加载子视图。调用messageTitle后,instantiateViewControllerWithIdentifier未初始化。调用self.messageTitle以在设置值之前初始化视图。

ErrorMessageViewController * errorMessage = (ErrorMessageViewController *)
[storyboard instantiateViewControllerWithIdentifier:@"ErrorMessage"];

//tempTitle = nil
// [errorMessage initTitle:@"Error" andContent:contentVar]; 

[[self navigationController] pushViewController:errorMessage animated:YES];
// set value after push
[errorMessage initTitle:@"Error" andContent:contentVar]; 

答案 1 :(得分:1)

像这样设置数据。

NSString * errorMessage = @"Duplicate ID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ErrorMessageViewController * errorMessage = (ErrorMessageViewController *)
[storyboard instantiateViewControllerWithIdentifier:@"ErrorMessage"];
errorMessage.messageTitle = @"Error";
errorMessage.messageContent = errorMessage;
[[self navigationController] pushViewController:errorMessage animated:YES];

并直接在viewDidLoadviewWillAppear

中使用这些变量
- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // Use your variables 
}

修改:

-(void) initTitle:(NSString *)title andContent:(NSString *)content {
    tempTitle = (title == nil ? @"" : title);
    tempContent = (content == nil ? @"" : content);
}

NSString * errorMessage = @"Duplicate ID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ErrorMessageViewController *vc = (ErrorMessageViewController *)
[storyboard instantiateViewControllerWithIdentifier:@"ErrorMessage"];
[vc initTitle:@"Error" andContent:errorMessage];
[[self navigationController] pushViewController:vc animated:YES];

希望这会有所帮助