使用xib,您可以调用不同的初始值设定项:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Fetch Note...
// Initialize Edit Note View Controller with the fetched Note
EditNoteViewController *vc = [[EditNoteViewController alloc] initWithNote:note];
// Push View Controller onto Navigation Stack
[self.navigationController pushViewController:vc animated:YES];
}
这允许我将变量(在EditNoteViewController中)保密,我也可以为某些变量设置默认值,例如。
- (id)initWithNote:(Note *)note {
// ....
if (self) {
self.note = note;
self.isEditing = YES;
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//...
if (self) {
self.isEditing = NO;
}
return self;
}
我现在正在尝试使用故事板:
请尽可能明确
答案 0 :(得分:0)
您可以使用- initWithCoder:
无论您是从nib还是故事板实例化UIViewController,都会调用此方法。
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self) {
// initializations here
}
return self;
}
答案 1 :(得分:-1)
您应该使用prepareForSegue。仅在接收视图控制器中公开您需要的内容
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Prepare for next view controller
if ([segue.identifier isEqualToString:@"someThing"]) {
SomeThingViewController *viewController = segue.destinationViewController;
viewController.someProperty = @"something else";
}
}
属性someProperty需要在SomeThingViewController的标头中公开
@property (nonatomic, strong) NSString *someProperty;
要设置默认值,请检查接收视图控制器的viewDidLoad中的属性值
if (someProperty==nil) someProperty = @"Default";