开发者

In iPhone development how to access a method in the ParentViewController

I am a newbie iPhone Programmer and have a question regarding how to access methods of a Parent View Controller. In my program when the program first loads (applicationDidFinishLaunching) I do the following code:

[window addSubview:rootViewController.view];

[window makeKeyAndVisible];

which basically calls this

- (void)viewDidLoad {

    HomeViewController *homeController=[[HomeViewController alloc] initWithNibName:@"HomeView" bundle:nil];
    sel开发者_高级运维f.homeViewController=homeController;
    [self.view insertSubview:homeController.view atIndex:0];
    [homeController release];
    [super viewDidLoad];
}

Now, I have an IBAction call on HomeViewController that I want to have it call a method in root View Controller

I want to call this method

- (void)loadNewGame
{
    self.questionViewController = [[QuestionViewController alloc] initWithNibName:@"QuestionView" bundle:nil];
 //[homeViewController.view removeFromSuperview];    
    [self.view insertSubview:questionViewController.view atIndex:0];
}

So my question is how do I call a method from the Parent View controller?

I've tried

 [self.view removeFromSuperview];

 [self.parentViewController loadNewGame];

but that doesn't seem to work. Could someone please ploint me in the right direction.

Thanks in advance

Scott


First off, typically you call [super viewDidLoad] first in your viewDidLoad.

You will have to have an instance variable in your homeController class for your rootViewController. Then you could have a method in homeController:

- (void) loadNewGame
{
    [self.rootViewController loadNewGame];
}

This is one of many different ways to accomplish this. You may want to move the method to homeController completely. Or you may wish to have IB use the rootViewControllers' methods directly...

Here is another discussion of this.


First off your code doesn't really make sense. Why are you adding your HomeViewController in a -viewDidLoad call. If you want to load the HomeViewController as the initial view, you should set that in Interface Builder instead of RootViewController. When you want to display a new view controller, you should be using a navigation controller stack and pushing the new view controller onto it with [[self navigationController] pushViewController:newViewController animated:YES].

Assuming you get that sorted, you should create a delegate (id) field for your child view controller that you can set when you instantiate the new view controller. So your code might look something like this:

HomeViewController *homeController=[[HomeViewController alloc] 
                 initWithNibName:@"HomeView" bundle:nil];
[homeController setDelegate:self];
[[self navigationController] pushViewController:homeController animated:YES];
[homeController release];

Then, when your action gets fired in the HomeViewController, you can check to see if the delegate is set and if so, call the selector in question, like this:

- (IBAction)action:(id)sender;
{
    if (delegate && [delegate respondsToSelector:@selector(loadNewGame)])
        [delegate performSelector:@selector(loadNewGame)];
}

You might want to read Apple's docs on how to use the navigation controller stack. This might help clarify some things.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜