【问题标题】:Modal View reappearing + crashing: "Attempting to transition while a transition is in progress"模态视图重新出现 + 崩溃:“在转换过程中尝试转换”
【发布时间】:2012-04-10 15:04:34
【问题描述】:

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

现在的问题是:在我们希望支持的 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: 的位置以及视图被关闭的时间。

【问题讨论】:

    标签: iphone ios ios4 modalviewcontroller


    【解决方案1】:

    在我发布赏金后的几个小时内我会解决的数字!

    问题是 fetchEntries 方法(我没有发布因为我认为它不是罪魁祸首)在完成对服务器的调用时将我的分段控件的选定索引设置为 -1。如果将 EventValueChanged 更改为 -1,则似乎较新版本的 iOS 会忽略它。我只是设置了一个条件来忽略 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];
        }
    
    }
    

    【讨论】:

    • 感谢您发布您的解决方案。提供赏金通常会引发心理触发,让您更加努力地思考并自己找到解决方案,所以无论如何不要为“失去”的声誉感到抱歉:)
    【解决方案2】:

    您没有从-(void)viewWillAppear:(BOOL)animated 内部呼叫super

    尝试在顶部添加以下行:

    [super viewWillAppear:animated];
    

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

    【讨论】:

    • 虽然这确实是我错过的东西,但它并没有解决崩溃问题。
    • 崩溃信息还是一样吗?您是否可能试图在呈现视图控制器时关闭/呈现视图控制器?您是否尝试关闭所有动画?
    • 崩溃消息是一样的。 - 没有其他会干扰的视图被管理。排序时重新加载的表格视图的数据可能是罪魁祸首,但我仍然不知道何时/何处排序不会干扰任何动画。 - 呈现不带动画的模态视图控制器会导致根本不显示模态视图。关闭动画以解除关闭导致我需要连续 3 次点击模态视图上的选择以关闭视图,并且父视图在返回屏幕时偏离中心。非常奇怪的行为。
    猜你喜欢
    • 2014-12-23
    • 1970-01-01
    • 2011-10-20
    • 2020-11-02
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多