我有一个班级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:
时,tempTitle
和tempContent
为NSCFConstString
"错误"和NSCFString
"重复ID"。
然后流程转到viewWillAppear
,tempTitle
和tempContent
的内容都消失了,取而代之的是NSCFString
和{{1} }。 tempTitle分配仍然正常,因为它仍然是字符串,但我不知道内容是什么,但tempContent分配产生错误。我甚至无法记录NSObject
值。
为什么会这样?为什么对象的内容会发生变化,就像它从未初始化一样?
答案 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];
并直接在viewDidLoad
或viewWillAppear
。
- (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];
希望这会有所帮助