Bradley
Bradley

Reputation: 617

Refreshing UInavigationbar with segmented control

I have a login/signup page that is changed using a segmented control. The login page is generic and the self.title = @"login" and the bar item = login to send login request. However when the user selected the signup segmented control I want to refresh the navigation bar to display the self.title = @"Sign Up" and the bar button = sign up to send a register request. I have used this code in the segmented control is:

if (segmentedControl.selectedSegmentIndex == 0) 
    {
        UIBarButtonItem * sortButton = [[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleBordered target:self action:@selector(LoginButton)];

        self.navigationItem.rightBarButtonItem = sortButton;
        [sortButton release];

        self.title = @"Login";
    }
    else if (segmentedControl.selectedSegmentIndex == 1)
    {
        UIBarButtonItem * sortButton = [[UIBarButtonItem alloc] initWithTitle:@"Submit" style:UIBarButtonItemStyleBordered target:self action:@selector(Submit)];

        self.navigationItem.rightBarButtonItem = sortButton;
        [sortButton release];

        self.title = @"Submit";

    }

I also have the segmented control set up and working :

- (IBAction)segmentSwitch:(id)sender 
{
    UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
    NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

    if (selectedSegment == 0) 
    {
        //toggle the correct view to be visible
        [firstView setHidden:NO];
        [secondView setHidden:YES];
    }
    else
    {
        //toggle the correct view to be visible
        [firstView setHidden:YES];
        [secondView setHidden:NO];
    }
}

Andy help would be appreciated. Thanks.

Upvotes: 0

Views: 577

Answers (1)

conmulligan
conmulligan

Reputation: 7148

Assuming the first block of code is located in the viewDidLoad method, you'll need to change your UIViewController so that the title and button are altered when the UISegmentedControl's value changes. In other words, try moving the first block of code to the segmentSwitch: method.

The reason why your code isn't working is because viewDidLoad only gets called once, and so the first block of code is only executed once, while you want it executed every time the user selects a new segment.

Upvotes: 1

Related Questions