Quentin Hayot
Quentin Hayot

Reputation: 7876

Accessing a class' running instance from the AppDelegate

New to Obj-C, I'm trying to trigger a method myMethod of an instance of viewControllerB. =>viewControllerB is instanciated in viewControllerA. =>I'm trying to call myMethod from the AppDelegate =>It seems that anyway I try to do that, It's triggering myMethod in a new instance of viewControllerB

So the question is: how to send a message to an existing instance of a viewController ?

Please explain with code samples since I'm not yet really comfortable with Obj-C.

Thanks !

viewControllerA.m :

if (_viewControllerB == nil) {
    self.viewControllerB = [[[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:[NSBundle mainBundle]] autorelease];
}

viewControllerB.m :

- (void)myMethod{ NSLog(@"myMethod!"); }

AppDelegate.m : Don't know what tu put here :(

Upvotes: 0

Views: 145

Answers (2)

saadnib
saadnib

Reputation: 11145

To get same instance

for (UIViewController *view in self.navigationController.viewControllers)

{

if(view isKindOfClass:[viewControllerB class])
{
     [view yourmethodname];
}

}

Upvotes: 1

saadnib
saadnib

Reputation: 11145

if you want to call the function on the same instance of your viewControllerB then you have to take out that instance from where you added it.

Upvotes: 0

Related Questions