PDA

View Full Version : Invoking an IBAction from the root controller




happybob
Apr 29, 2009, 09:39 AM
Hi All, I have an app with two views, a settings view and a game view. The toggle view button command (IBAction) is located in the rootViewController. While going from the settings view to the game view is easy the problem arises when going from the game view to the settings view when the game is over. This is because I want to invoke the toggle view Action when the game is over without having the button pressed. But since the command is located in the rootviewcontroller there's a problem. Is there a way around this where I can activated the IBAction in code?



ghayenga
Apr 29, 2009, 10:35 AM
Hi All, I have an app with two views, a settings view and a game view. The toggle view button command (IBAction) is located in the rootViewController. While going from the settings view to the game view is easy the problem arises when going from the game view to the settings view when the game is over. This is because I want to invoke the toggle view Action when the game is over without having the button pressed. But since the command is located in the rootviewcontroller there's a problem. Is there a way around this where I can activated the IBAction in code?

There's all sorts of ways. The two main ones being get a reference to your rootviewcontroller and say [myrootviewcontroller doToggleAction]; or use the notification manager and have your rootviewcontroller register to receive the notification, and execute your doToggleAction method when it receives it, and have your game view controller post said notification.

happybob
Apr 29, 2009, 10:42 AM
Thanks for the response. I did something along those lines. In the views header file I inserted the class rootviewcontroller. Imported the rootviewcontroller header file into the .m. Then wrote [rootViewController toggleView];

I tried that before and it didn't work. Am I missing something simple?

BlackWolf
Apr 29, 2009, 10:52 AM
Thanks for the response. I did something along those lines. In the views header file I inserted the class rootviewcontroller. Imported the rootviewcontroller header file into the .m. Then wrote [rootViewController toggleView];

I tried that before and it didn't work. Am I missing something simple?

yeah, you toggled the view for AN rootViewController, but not the one that you display on the screen.

I didn't really get your problem though.
you have 3 viewcontrollers. your rootViewController has an IBAction "toggleView" which is invoked when the game is over. why exactly do you need that somewhere outside of your rootViewController?

but in any case, if you need it somewhere else you have to get a reference to your rootViewController somehow. you could, for example, set an instance variable of your settingsViewController to the rootViewController in your appDelegate or wherever you create/display those views.

happybob
Apr 29, 2009, 01:31 PM
Thanks for the answers but I'm still having a little trouble. I'm from an embedded C background. I'm trying to convert a few of my designs over to the iphone, so the structure is a little new and I'm still getting used to it.

To simplify what I'm trying to do: I want to invoke the IBAction function toggleView located in the RootViewController at the end of the GameView implementation file. I've got the RootViewController class declared in the header and am importing the file into the GameView implementation file. Then write [RootViewController toggleView];

I've done some more research and have tried quite a few ways but nothing seems to work. I get the concept of referencing the RootViewController in the app delegate just don't know what the correct syntax should be. I can't seem to find an example of it being implemented.

BlackWolf
Apr 29, 2009, 03:02 PM
Thanks for the answers but I'm still having a little trouble. I'm from an embedded C background. I'm trying to convert a few of my designs over to the iphone, so the structure is a little new and I'm still getting used to it.

To simplify what I'm trying to do: I want to invoke the IBAction function toggleView located in the RootViewController at the end of the GameView implementation file. I've got the RootViewController class declared in the header and am importing the file into the GameView implementation file. Then write [RootViewController toggleView];

I've done some more research and have tried quite a few ways but nothing seems to work. I get the concept of referencing the RootViewController in the app delegate just don't know what the correct syntax should be. I can't seem to find an example of it being implemented.

ah, now I get it.
like I said, that won't work.
you create an instance of RootViewController and display it. if you create some other rootviewcontroller in your gameView, that has nothing to do with the rootviewcontroller you displayed - because they are two different objects.

I don't know the exact structure of your app, so it's kinda hard to say, but I guess if you need your toggleView method outside of your rootviewcontroller, it shouldn't be a method of your rootviewController, because in that case it doesn't belong to the rootviewController!

if it belongs to your rootviewcontroller for some reason, in your app delegate where you create your view controllers, you simply put in something like

gameViewController.referenceToRootViewController = rootViewController;


of course you need to create an instance variable "referenceToRootViewController" in your gameView's header!

so the principle of the thing is: store the rootViewController in an instance variable of the gameViewController as long as you can easily access it.

happybob
Apr 29, 2009, 04:19 PM
The application is structured as follows:

A RootViewController to controller the switching of views.
A LoadViewController which initially loads to control the settings
A GameView in which the actions take place

**If it helps the structure is based off the Utility template that comes with the SDK**

So I placed the following code in the AppDelegate.m

#import "GameView.h"

@synthesize gameView;

-(void) applicationDidFinishLaunching:(UIApplication *)application {
gameView.referenceToRootViewController = rootViewController;
}


GameView.h

@interface GameView : UIView {

referenceToRootViewController *referenceToRootViewController;

}

@property (nonatomic, retain) referenceToRootViewController *referenceToRootViewController;


GameView.m

#import "RootViewController.h"

@synthesize referenceToRootViewController;

-(void)endGame {
[rootViewController toggleView];
}


Is there a solution or an easier way to call the other view?

BlackWolf
Apr 30, 2009, 12:32 AM
The application is structured as follows:

A RootViewController to controller the switching of views.
A LoadViewController which initially loads to control the settings
A GameView in which the actions take place

**If it helps the structure is based off the Utility template that comes with the SDK**

So I placed the following code in the AppDelegate.m

#import "GameView.h"

@synthesize gameView;

-(void) applicationDidFinishLaunching:(UIApplication *)application {
gameView.referenceToRootViewController = rootViewController;
}


GameView.h

@interface GameView : UIView {

referenceToRootViewController *referenceToRootViewController;

}

@property (nonatomic, retain) referenceToRootViewController *referenceToRootViewController;


GameView.m

#import "RootViewController.h"

@synthesize referenceToRootViewController;

-(void)endGame {
[rootViewController toggleView];
}


Is there a solution or an easier way to call the other view?

you should REALLY learn objC basics again!
but because I'm in a good mood I'll give you a hint:
in your scenario, the content of your "referenceToRootViewController" variable would be of class "referenceToRootViewController" - but there is no such class, so it will not work. what you really want to store in there is a rootViewController.

oh and btw: this IS a pretty easy solution.

happybob
Apr 30, 2009, 03:32 PM
Got it working. I just started from scratch in a blank file and got it to work. Then just imported everything. Thanks.