简体   繁体   中英

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. If I do login.view than I got a warning that value of login.view is not used.

So what should I do?

If you don't care about the return value, then don't use the property accessor syntax ( login.view ). Call the getter like a regular method: [login view] .

If you just want use login.view; to make the viewDidLoad be called , I think you had better to add some code in your BGLogin classs init method.

such as :

- (id)init
{
    // your code

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

or you can change your login.view; to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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