简体   繁体   中英

Can't initializing a delegate in the AppDelegate but inside a class method it works

I am trying to initialize a delegate ( MyDelegate ) in the AppDelegate inside the didFinishLaunchingWithOptions method as follows:

UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
[[someClassName instance] setMyDelegate:[myStoryboard instantiateViewControllerWithIdentifier:@"someVcName"]];

While debugging, all the calls perform well but when it returns to the AppDelegate after the above calls, MyDelegate is nil.

What is interesting is that when I try to set the delegate inside a class method I wrote, it works - the exact same code above.

Why the initializing doesn't work in the AppDelegate while it works in the class method ?

Thanks.

EDIT :

Basically @Leo solved the problem but the solution was really straightforward since I was using Storyboard then the first ViewController is automatically initiated (assuming you assigned the entry point arrow icon):

The solution:

[[someClassName instance] setMyDelegate:[self.window rootViewController]];

About method instantiateViewControllerWithIdentifier

From the document

This method creates a new instance of the specified view controller each time you call it.

So,in didFinishLaunchingWithOptions ,you create a local viewController instance.

And,it will be dealloced after the method is done.

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