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.
Assuming the first block of code is located in the
viewDidLoadmethod, you’ll need to change yourUIViewControllerso that the title and button are altered when theUISegmentedControl‘s value changes. In other words, try moving the first block of code to thesegmentSwitch:method.The reason why your code isn’t working is because
viewDidLoadonly 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.