我有一个UIViewController
(iOS),在创建它的视图中设置了bool属性。
我可以为属性设置默认值吗?
如果我把它放在ViewDidLoad
中,即使我在推送ViewController
@ Erakk的一些代码要求找出问题:
@implementation CreateMomentViewController
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
// NOT GETTING EXECUTED
CreateMomentViewController *me = [super initWithNibName:nibName bundle:nibBundle];
me.exitButton = YES;
return me;
}
- (void)viewDidLoad {
[super viewDidLoad];
// self.exitButton = YES;
// Do any additional setup after loading the view.
}
来电者:
ChefSelectionViewController
是CreateMomentViewController
ChefSelectionViewController *nextScreen = [self.storyboard instantiateViewControllerWithIdentifier:@"ChefSelect"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController pushViewController:nextScreen animated:YES];
});
答案 0 :(得分:3)
尝试将initWithNibName更改为:
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
self.exitButton = YES;
}
return self;
}
此外,我不认为你需要一个viewController的子类来创建它,但我不知道你的流程如何工作所以它只是一个评论=)
顺便说一下,您要通过@"ChefSelect"
名称实例化的视图,它的类(在xib / interface builder中)应该与CreateMomentViewController
相同。您可能实际上正在实例化ChefSelectionViewController
,并且永远不会调用CreateMomentsViewController
上的代码。
答案 1 :(得分:3)
我遵循此标准来设置默认值。
- (void)customInit {
// Default Values
self.exitButton = NO;
}
- (id)init {
if (self = [super init]) {
[self customInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self customInit];
}
return self;
}
...
...
答案 2 :(得分:1)
我会覆盖-initWithNibName:bundle:
并将其放在那里。默认值属于初始值设定项。请务必致电[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]
。