Let me provide some context: I am building a tabbed application that allows a user to find and view some videos hosted on our server. Each tab has the videos grouped in a different manner with a segmented control in the navigation bar that a user can use to sort the list even more precisely (by title, date, etc…). Upon hitting “Sort” in the segmented control, a modal view controller is presented with the options available on a particular tab. An option is chosen and the choice is relayed back to parent view controller, which calls on the server for a sorted list.
Now here is the problem: On iOS 4.2, which we would like to support, the modal view either crashes after a sort option has been chosen, or it dismisses and then immediately reappears one more time. If it reappears, it only does so once and does NOT loop indefinitely. I know it has something to do with the transition and the view’s life cycle but I can’t seem to get this just right.
Code:
The parent view
-(void) segmentAction:(id)sender{
//create a sort view and pass it a value that indicates what the options should be
ModalSortViewController *sortView = [[ModalSortViewController alloc]
initWithNibName:nil bundle:nil sortByView:0];
[sortView setDelegate:self];
[sortView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[sortView setModalPresentationStyle:UIModalPresentationFormSheet];
[self presentModalViewController:sortView animated:YES];
}
-(void) refresh:(id)sender{
[self fetchEntries];
}
//Delegate protocol for all tabbed table views
//Receives buttonIndex from the modal sort view
-(void)sortByButtonIndex:(int)buttonIndex{
if(buttonIndex==1){
//If sorting by title
fetchURL = @"fakeURL.com/?method=iGetCategories&sortBy=category&sortByOrder=ASC";
[self fetchEntries];
}
else if (buttonIndex==2){
//If sorting by number of items
fetchURL = @"fakeURL.com/?method=iGetCategories&sortBy=count&sortByOrder=DESC";
[self fetchEntries];
}
else if(buttonIndex==0){
//Resets sort selection to nothing
segmentedControl.selectedSegmentIndex = -1;
}
[self dismissModalViewControllerAnimated:YES];
}
The modal view
@synthesize delegate, option1, option2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil sortByView:(int)_viewInt
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
sortChosen = 0;
viewInt = _viewInt;
}
return self;
}
//This method is called whenever a selection on the modal view has been made.
//The button tags have been set in IB and are sent to the parent table view controller
//where a switch statement is in place to sort its data by the selection.
-(IBAction)madeSelection:(id)sender{
sortChosen = [sender tag];
[self.delegate sortByButtonIndex:sortChosen];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];//Added after Felix pointed out that the super was not called
switch (viewInt) {
case CAT_FOLDERS:
[self.option1 setTitle:@"By Category Name" forState:UIControlStateNormal];
[self.option2 setTitle:@"By Number of Items" forState:UIControlStateNormal];
break;
case PRES_FOLDERS:
[self.option1 setTitle:@"By Presenter Name" forState:UIControlStateNormal];
[self.option2 setTitle:@"By Number of Items" forState:UIControlStateNormal];
break;
case MEDIA:
[self.option1 setTitle:@"By Media Title" forState:UIControlStateNormal];
[self.option2 setTitle:@"By Release Date" forState:UIControlStateNormal];
break;
default:
break;
}
}
Crash Results:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Attempting to begin a modal transition from <UINavigationController:
0x139160> to <ModalSortViewController: 0x172810> while a transition is already in
progress. Wait for viewDidAppear/viewDidDisappear to know the current transition has completed'
Sorry about the length. I wanted to be as clear and thorough as possible. Thank you in advance!
EDIT: I should mention that the crashing/repeat appearing seems to be dependent on where sortByButtonIndex: is called and when the view is dismissed.
Figures I would solve this hours after I posted a bounty on it!
The problem was that the fetchEntries method, which I did not post because I didn’t think it was the culprit, sets my segmented control’s selected index to -1 when it completes its call to the server. It appears that newer versions of iOS ignore the the EventValueChanged if it is changing to -1. I simply set a condition to ignore a -1 index on the segmented control in segmentAction: method and it works.