tags:

views:

1702

answers:

3

Hi All, Sorry - this may be an easy question, I'm new to iPhone development and still wrapping my head around Views vs ViewControllers.

I have a NavigationViewController and I can push Views using the following method in the RootViewController which is connected to a Bar Button Item:

- (IBAction)switch:(id)sender {
        NSLog(@"Swith...");
        LibraryViewController *varLibraryViewController = [[LibraryViewController alloc] initWithNibName:@"LibraryViewController" bundle:nil];
        [[self navigationController] pushViewController:varLibraryViewController animated:YES];
    }

I want to call this same method from a button on the same view that is currently loaded. Basically I want to have the Bar Button at the top call the same method as a button on the view. I was wondering how to call a method in the ViewController from the view loaded from that viewController. Hopefully that makes sense.

Do I need to create an instance of the RootViewController? I would think that would already be instantiated. Thank you.

+1  A: 

You'll want to declare your method as an IBAction in your header file:

- (IBAction) myMethod: (id) sender;

Save your header, then switch to Interface Builder. Right click on the Bar Button, and drag from the selector tag to your view controller object (probably the File Owner). When you release, you should be given a popup menu of available actions, and myMethod should be selectable.

If you don't get this popup, you may need to make sure your File Owner class is set properly: select the File Owner in the file window, then select "Tools" > "Identity Inspector" from the menu. In the inspector, type your view controller's class into the Class field.

Ben Gottlieb
I think he said the bar button is already connected to it
Kevin Ballard
+3  A: 

BTW, the code you have pasted there is leaking your LibraryViewController. You need to either explicitly release it after pushing it, or autorelease it when it's created.

Kevin Ballard
+1  A: 

Your RootViewController should have its own xib file. In this xib, the RootViewController is represented by the object named "File's Owner". You can link buttons on the view to File's Owner the same way you can link things to RootViewController in MainMenu.xib.

Kevin Ballard
Ben's answer also helped but your answer was more direct to what I needed.
Sean