开发者

UINavigation Bar Button, change IBAction depending on click

Ok so at the moment i have a navigation controller with a right button which logs a user in.

however I want to change the login button to logout once it has been clicked and this logout button will call a different IBAction.

to help visualize this.

As default, I have a right button on the navigation controller which says login, on开发者_如何学运维ce this login button is pressed the ibaction login:(id)sender is pressed.

what i want to do is change the button to logout and call logout:(id)sender when it is clicked.

is this possible.

thanks.


You can just change what the button does when it is pressed:

- (void)login:(UIButton*)button {
    [button setTitle:@"Logout"];        
    [button setAction:@selector(logout:)];
}

- (void)logout:(UIButton*)button {
    [button setTitle:@"Login"];     
    [button setAction:@selector(login:)];
}

Alternately, since the button is on the UINavigationBar, you could instead do this:

-(IBAction)login:(id)sender{
UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                            target:self
                                                                            action:@selector(logout:)];
[[self navigationItem] setRightBarButtonItem:logoutButton];
[logoutButton release];
}

-(IBAction)logout:(id)sender{
UIBarButtonItem *loginButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                            target:self
                                                                            action:@selector(login:)];
[[self navigationItem] setRightBarButtonItem:loginButton];
[loginButton release];
}


You can set two different titles or image for the button, one for normal/default state and other for selected state.

Similarly, you can set two different operation based on the state of the button. So, if button is in normal/default state, user needs to login. Once user does login, we put the button into selected state.

Thus, if button is in selected state, user needs to log out.

In this way, we can keep toggling the states of the same button, to do two different operations.

The code would look something like this.

-(IBAction) loginButtonPressed:(id) sender {
    UIButton *loginButton = (UIButton *) sender;

    if (loginButton.selected == NO) {
        // Represents user needs to login. Code for login user.
    }else
        // Represents user needs to logout. Code for logout user.
    }

    // toggle the login/logout states.
    loginButton.selected = !loginButton.selected;
}

You can specify titles/images for the button for normal/selected/highlighted/disabled states. This would do your job with single button.


You can change the target action by using addTarget and removeTarget, as stated in this related question.

The problem here is to have an elegant way to detect the status of being logged in or not. You can think of a global variable, but it's a bad solution most of the times.

Another alternative is inspecting the UIButton's NSString *title property.

A nice approach is to have a sort of 'session' mechanism as in web applications, which will give you the status application-wide.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜