模态视图重新出现+崩溃:“在转换过程中尝试转换”

时间:2012-04-10 15:04:35

标签: iphone ios ios4 modalviewcontroller

让我提供一些背景信息:我正在构建一个标签式应用程序,允许用户查找和查看我们服务器上托管的一些视频。每个选项卡都以不同的方式对视频进行分组,并在导航栏中使用分段控件,用户可以使用该控件对列表进行更精确的排序(按标题,日期等)。在分段控件中点击“排序”时,会在模式视图控制器上显示特定选项卡上可用的选项。选择一个选项,并将选择中继回父视图控制器,后者在服务器上调用排序列表。

现在问题就出现了:在我们想要支持的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:的位置以及何时解除视图。

2 个答案:

答案 0 :(得分:2)

在我发布奖金之后,我会解决这个问题!

问题在于fetchEntries方法,我没有发布,因为我认为它不是罪魁祸首,它在完成对服务器的调用时将我的分段控件的选定索引设置为-1。如果更新为-1,似乎较新版本的iOS会忽略EventValueChanged。我只是设置一个条件来忽略segmentAction:方法中分段控件的-1索引,它可以工作。

-(void) segmentAction:(id)sender{

    if(segmentedControl.selectedIndex != -1){
        //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];
    }

}

答案 1 :(得分:1)

您未在super内致电-(void)viewWillAppear:(BOOL)animated

尝试在顶部添加以下行:

[super viewWillAppear:animated];

这可能意味着ViewController的超级实现没有正确设置其显示标志。