4

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

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

4

2 回答 2

2

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

问题是我没有发布的 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];
    }

}
于 2012-04-13T21:22:40.817 回答
1

你不是super从内部调用-(void)viewWillAppear:(BOOL)animated

尝试在顶部添加以下行:

[super viewWillAppear:animated];

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

于 2012-04-10T15:14:46.557 回答