KerrM
KerrM

Reputation: 5240

UIBarButtonItem assigned action in Storyboard but not executing

So I have a UIBarButtonItem that I put on a screen via Storyboard. When I click on it, the screen should go to another view. This works fine. I also want to do some processing when the user clicks on the button. I've linked the button with the view controller and under "Sent Actions" selected the function I want to run when the button is pressed.

Normally, I would use IB to select Touch Up Inside and select the action there but I can't find that anywhere (it's also the first time I use a UIBarButtonItem so I don't even know if that is an option here or not).

I guess the main question is: how do I link a UIBarButtonItem to a function so that when the user clicks on it, the application runs that function and then moves on to the next view? All of this in XCode 4.2...

EDIT: Bit of a typo there - meant UIBarButtonItem not UITabBarButton - been a long day!

Thanks,

Upvotes: 0

Views: 4697

Answers (4)

SteveD430
SteveD430

Reputation: 23

As an addendum to the previous answer from ascorbic

If you select the View Controller and then open up the connections dialogue on the right, you will see under the Received Actions section an entry to the non-attached Action. If you Click that entry and drag it to the Toolbar button then that will make the connection.

Upvotes: 2

askoric
askoric

Reputation: 61

You can link Bar Button Item to an IBAction in Storyboard. The trick is to link ViewController's (not First Responder's!) Receiver Action to the Bar Button Item. If you ctrl + drag the Action from the First Responder, the IBAction will remain unlinked (you can check the hollow circle by the IBAction in your ViewController's .h file). Weird, I know, but it worked for me.

Upvotes: 0

Padin215
Padin215

Reputation: 7484

-(IBAction)switchToScoutingForm:(id) sender
{
    //run the needed code

    MySecondViewController *myVC = [self.storyboard instantiateViewControllerWithIdentifier:@"myVC"];

    [self setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:myVC animated:YES];
}

then use the UIBarButtonItem's selector to link to this method. it will then pass the UIButton (if you need it), run the code you have, and then do a modal transition to your other view.

EDIT: replace MySecondViewController with the name of the view controller you want to transition to. also be sure to import the other view controller in your .h file:

#import "MySecondViewController.h"

Upvotes: 0

jrturton
jrturton

Reputation: 119262

If the button will always take you to the next view (that is, there is no validation that needs to be performed on the current view before you can leave) then you can call your function from prepareForSegue:. Use segue.identifier to work out which segue you are dealing with.

Upvotes: 2

Related Questions