让我先提供一些背景信息:我正在构建一个选项卡应用程序,允许用户查找和查看托管在我们服务器上的视频。每个选项卡以不同的方式对视频进行分组,导航栏中有一个分段控件,用户可以使用它更加精确地排序列表(按标题、日期等)。点击分段控制中的“排序”后,会呈现一个模态视图控制器,其中包含特定选项卡可用的选项。选择一个选项后,将选择的内容传递回父视图控制器,然后调用服务器以获取已排序的列表。
现在出现了问题:在iOS 4.2上,我们希望支持该版本,模态视图在选择排序选项后要么崩溃,要么在消失后立即再次出现。如果再次出现,则只会出现一次,而不会无限循环。我知道这与过渡和视图的生命周期有关,但我似乎无法做到完美地解决这个问题。
代码如下:
父视图
现在出现了问题:在iOS 4.2上,我们希望支持该版本,模态视图在选择排序选项后要么崩溃,要么在消失后立即再次出现。如果再次出现,则只会出现一次,而不会无限循环。我知道这与过渡和视图的生命周期有关,但我似乎无法做到完美地解决这个问题。
代码如下:
父视图
-(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];
}
模态视图
@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;
}
}
崩溃结果:
*** 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'
很抱歉内容有点长,但我想尽可能地清晰和详尽。先提前感谢您!
编辑提示:需要注意的是,崩溃/重复出现似乎取决于sortByButtonIndex:何时被调用以及何时视图被解散。