views:

472

answers:

3

Hi All,

I am working on a view based application. In this i have two viewcontrollers. They are: Rootviewcontroller and Detailviewcontroller.

Now i am passing a values to Detailviewcontroller using pushViewController method.It works fine. But now when i pop out from this Detailviewcontroller i should access myfunction in Rootviewcontroller. Is it possible please suggest?

Any help would be apprecited !

-Sathiya

+4  A: 

call myfunction in the root view controller's viewWillAppear:(BOOL)animated method or the viewDidAppear:(BOOL)animated method, depending on when you want it to be called. that way whenever that controller appears the code you need will be run.

Kevlar
A: 

I don't really understand your question 100%, but I think you want to access the methods of the Root ViewController from another View Controller further up the stack?

You can do something like this in DetailViewController:

RootViewController *rootViewController = (RootViewController*)self.navigationController.topViewController;

Now that you have a reference to the Root, you can call methods on it as you wish.

bpapa
Yes i want to access the methods of the Rootviewcontroller from another view Controller. As per your suggestion i tried and the rootviewcontroller methods but the application gets terminated. Is there any other solution?
Sathiya
A: 

Store instance of rootViewController in AppDelegate.

// AppDelegate.h

RootViewController* rootViewController;

Add an access method to the implementation.

AppDelegate.m


- (UIViewController*) GetRootViewController {
 return rootViewController;
} 

Call this method to get the rootViewController from anywhere in your app.

UIViewController* controller = [(AppDelegate*)[[UIApplication sharedApplication] delegate] GetRootViewController];
timbos