Search code examples
ios6uiviewcontrolleruibuttonuinavigationbar

UIBarButtonItem as a BackButton


I have two view controllers. The first is the login page and the second is the user cabinet. When I sign in and go to second view I can see back button "FirstController" at the Navigation Bar. But I don't need of this button and I want to have rect button as a "Sign Out".

Question: how can I set rect button "Sign Out" as a back button?


Solution

  • Hide UINavigationbar's Hide button while requesting to load from login page & while loading "FirstController", initialize your UIBarButtonItem with custom view - UIButton (Sign Out button) and also set selector to respond as click on "Sign Out".

    Sample Code:

    To Hide back button:

    self.navigationItem.hidesBackButton = TRUE;

    To add "Sign Out" button:

    -(void)setMySignOut
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"btn_logout.png"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"btn_logout_h.png"] forState:UIControlStateHighlighted];
        button.frame = CGRectMake(0, 0, 70, 30);
        [button addTarget:self action:@selector(btnLogoutClicked:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:button];
    }
    
    -(void)btnLogoutClicked:(UIButton *)sender
    {
        // your logic goes here...
    }