简体   繁体   English

如何确保在没有警告的情况下调用viewDidLoad?

[英]How do I ensure that viewDidLoad is called without warning?

BGLogin * login = [[BGLogin alloc]init];

[nav pushViewController:login animated:YES];
login.view; //access it once to trigger didload
NSAssert(login.navigationItem==login.navigationItemAtDidload,@"They should be the same");//View didload isn't called yet here.
NSAssert(login.navigationController == nav,@"The navigation controller should be the same");
NSAssert(nav.navigationBar.topItem==login.navigationItem,@"We just push our self so our stuff should be on top");

It's simple code. 这是简单的代码。 However if I do not do login.view then viewDidLoad will not be called first. 但是,如果我不执行login.view,则不会首先调用viewDidLoad。 If I do login.view than I got a warning that value of login.view is not used. 如果我执行login.view,则收到警告,提示未使用login.view的值。

So what should I do? 所以我该怎么做?

If you don't care about the return value, then don't use the property accessor syntax ( login.view ). 如果您不关心返回值,则不要使用属性访问器语法( login.view )。 Call the getter like a regular method: [login view] . 像常规方法一样调用getter: [login view]

If you just want use login.view; 如果您只想使用login.view; to make the viewDidLoad be called , I think you had better to add some code in your BGLogin classs init method. 为了调用viewDidLoad ,我认为最好在BGLogin类的init方法中添加一些代码。

such as : 如 :

- (id)init
{
    // your code

    self.view.tag = 0;
    return self;
}

or you can change your login.view; 或者您可以更改您的login.view; to login.view.tag = 0; login.view.tag = 0;

it will call the viewDidLoad , but I do not think it is better , but it will work for you , maybe somebody has a better way to handle it 它会调用viewDidLoad ,但我认为它不是更好,但是它将为您工作,也许有人有更好的方法来处理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM