我有一个 UIViewController
(iOS) 具有从创建它的 View 设置的 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];
});
最佳答案
尝试将您的 initWithNibName 更改为:
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
self.exitButton = YES;
}
return self;
}
另外,我认为您不需要 viewController 的子类来创建它,但我不知道您的流程是如何工作的,所以它只是一个评论 =)
顺便说一句,您正在以
@"ChefSelect"
的名称实例化 View ,它的类(在 xib/界面生成器中)应该与 CreateMomentViewController
相同.您可能正在实例化 ChefSelectionViewController
和你的代码 CreateMomentsViewController
永远不会被调用。
关于ios - 具有默认值的 Objective C UIViewController 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30577494/