PDA

View Full Version : Navigation button not working like I expected




John Baughman
Oct 19, 2009, 09:00 PM
I have a segmented control with 2 buttons set as the right side button. When I tap either of these buttons the navigation bar slides to the new view and the back button displays correctly, but then immediately slides again as if I were pushing another view on to the stack. For example if I am going from the People view to the Prefernces view, The back button goes like this...

People -> Preferences.

The proper view displays but the didDisplay delegate method does not fire. If I then click the back button which says Preferences, it slides back to a People button and the didDisplay delegate fires. Clicking the People button properly takes me back where I started.

My code looks like this...


//Code to create the buttons
segmentedControl = [ [ UISegmentedControl alloc ] initWithItems: nil ];

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[ segmentedControl insertSegmentWithTitle: @"Prefs" atIndex: 0
animated: NO ];
[ segmentedControl insertSegmentWithTitle: @"Add" atIndex: 1
animated: NO ];

[ segmentedControl addTarget:self
action:@selector(segmentedControlPressed:)
forControlEvents:UIControlEventValueChanged ];

UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc]
initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentItem;
[segmentItem release];

[self.navigationItem setHidesBackButton:YES animated:YES];
self.navigationItem.leftBarButtonItem = self.editButtonItem;



//code that pushes the view after clicking the prefs button in the segmented control

self.navigationController.navigationBarHidden = NO;

PrefsViewController *prefsViewController = [[[PrefsViewController alloc]
initWithNibName:@"PrefsViewController" bundle:nil] autorelease];

[self.navigationController pushViewController:prefsViewController
animated:YES];

prefsViewController.view.backgroundColor = self.view.backgroundColor;




Not sure where to start in figuring out this one. Maybe someone can point me in the right direction.

Thanks.
John



John Baughman
Oct 20, 2009, 02:30 PM
I set the target for the sgmentedControl to segmentedControlPressed for value changed events. In segmentedControlPressed I checked to see if selectedSegmentIndex == 0, if 0 called the Prefs action method and else ran the Add action method. This would have worked fine except that I was immediately resetting the selectedSegmentIndex to UISegmentedControlNoSegment which caused segmentedControlPressed method to run again because the segmentedControl selected value changed to -1. Since I was only checking if the selectedSegmentIndex was 0, the Add method was then called, pushing the add view on top of the prefs view. I did not even notice that it was the wrong view showing up.

Fixed it be uniquely checking the value of selectedSegmentIndex. Bad code and good code follows....


//BAD CODE!!

- (void)segmentedControlPressed:(id)sender
{
UISegmentedControl *sg = sender;

if (sg.selectedSegmentIndex == 0) {
[self prefs];

} else {
[self add];

}

sg.selectedSegmentIndex = UISegmentedControlNoSegment;

}



//GOOD CODE

- (void)segmentedControlPressed:(id)sender
{
UISegmentedControl *sg = sender;

if (sg.selectedSegmentIndex == 0) {
[self prefs];

} else if(sg.selectedSegmentIndex == 1) {
[self add];
}

sg.selectedSegmentIndex = UISegmentedControlNoSegment;
}