OhadM
OhadM

Reputation: 4803

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]];

Upvotes: 1

Views: 235

Answers (1)

Leo
Leo

Reputation: 24714

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.

Upvotes: 4

Related Questions