views:

47

answers:

1

I want to add a Bar button to the Navigation bar on a Navigation based app, the bar however is on the MainWindow and has no code behind so I Can't connect it to an IBAction.

how do i go about connecting it to an action ?

+2  A: 

You can add the button to the navigation bar programmatically like this and also connect it up to an action with the init code:

    UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:<UIBarButtonStyle that you want> target:self action:@selector(<some_action>:)];
    self.navigationItem.rightBarButtonItem = button;
    [button release];
Kennzo
seems to add the button but i hooked it up to an action called login with nothing in the body and when i click it i get this -[RootViewController login:]: unrecognized selector sent to instance 0x593d990
irco
Can you post some code? Might make for easier debugging.
Kennzo
This is my viewDidLoad UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonItemStylePlain target:self action:@selector(login:)];button.title = @"Hello";self.navigationItem.rightBarButtonItem = button;[button release];and my actual action is just empty -(IBAction)login{}
irco
Where is the login action? Is it also in RootViewController. Your target right now is self so the login action should be wherever your target is.
Kennzo
right, yes it is in the rootviewcontroller. And this is the console output2010-09-13 16:49:32.163 navigation2[1257:207] -[RootViewController login:]: unrecognized selector sent to instance 0x5930fb02010-09-13 16:49:32.166 navigation2[1257:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController login:]: unrecognized selector sent to instance 0x5930fb0'
irco
I see it now. There's a colon at the end. If you have an action with arguments, you need the colon. But since there's no arguments in login, you don't need it. So it should look something like this: UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(login)];
Kennzo