【问题标题】:Opening a new UIVIewController by first deactivating UIModalTransitionStylePartialCurl通过首先停用 UIModalTransitionStylePartialCurl 打开一个新的 UIVIewController
【发布时间】:2011-08-11 18:40:44
【问题描述】:

我有三个 UIViewController:MainViewController、CurledViewController 和 SecondayViewController。

在 MainViewController 上,我在 MainViewController 中有一个 UIButton,它通过以下方式显示 CurledViewController:

curled = [[CurledViewController alloc] init];
[curled setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:curled animated:YES];

从我被告知的文档中:

使用此转换呈现的模态视图本身会被阻止 呈现任何额外的模态视图。

当 CurledViewController 以这种方式显示时,这会阻止我打开 SecondaryViewController。我想做的是,在 CurledViewController 中选择 UIButton 时,关闭 curl 并打开 SecondaryViewController(无论是来自 CurledViewController 还是 MainViewController 的调用都没有关系)。关闭 SecondaryViewController 后,我希望重新打开 CurledViewController。

在 CurledViewController 中附加到 UIButton 的函数中,我尝试过:

- (void)showSecondary:(UIButton *)sender {
   [self.parentViewController dismissModalViewControllerAnimated:YES];
   SecondaryViewController *secondaryView = [[SecondaryViewController alloc] initWithNibName:@"Secondary" bundle:Nil];
   [self presentModalViewController:secondaryView animated:YES];
   ...
}

但我还是被告知,

Application tried to present a nested modal view controller while curled

如何以这种方式打开一个新的 UIViewController?

谢谢!

【问题讨论】:

    标签: iphone objective-c uiviewcontroller uimodaltransitionstyle


    【解决方案1】:

    这里的问题是您用于呈现 SecondaryViewController 的代码仍在从 CurledViewController 执行。另一种尝试的方法是创建一个 CurledViewControllerDelegate 协议。将 MainViewController 设为 CurledViewController 的委托,并从 showSecondary 调用您的委托方法。

    在 CurledViewController 中,您的方法可能如下所示:

    - (void)showSecondary:(UIButton *)sender {
        [self.delegate dismissCurledViewController:self];
    }
    

    在 MainViewController 中,您的委托方法可能如下所示:

    - (void)dismissCurledViewController:(CurledViewController *)controller {
        [self dismissModalViewControllerAnimated:NO];
        SecondaryViewController *secondaryView = [[SecondaryViewController alloc] initWithNibName:@"Secondary" bundle:nil];
        [self presentModalViewController:secondaryView animated:YES];
        …
    }
    

    编辑

    为了在新模态视图控制器的解除和显示上保持动画,您需要引入一个延迟,以便有足够的时间让第一个动画完成。您可以通过使用适当的延迟值调用performSelector:withObject:afterDelay: 来完成此操作。然而,这是一种容易出错的方法,因为它假定第一个动画将始终具有相同的持续时间。

    作为另一个问题中的Andrew Pouliot suggested,您还可以尝试在 MainViewController 中覆盖 viewDidAppear:,以便它查找一个标志来确定是否应显示 SecondaryViewController。这仍然会使用我上面提到的委托方法,但 MainViewController 会有以下区别:

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        if(showSecondaryViewController) {
            SecondaryViewController *secondaryView = [[SecondaryViewController alloc] initWithNibName:@"Secondary" bundle:nil];
            [self presentModalViewController:secondaryView animated:YES];
        }
    
        showSecondaryViewController = NO;
    }
    
    - (void)dismissCurledViewController:(CurledViewController *)controller {
        showSecondaryViewController = YES;  
        [self dismissModalViewController:YES];
    }
    

    【讨论】:

    • 我试过这个,但即使从dismissCurledViewController函数调用,secondaryView也不会显示也不会报告错误。我可以在这个函数中写入 NSLog,甚至可以弹出一个 UIAlertView,它在 curl 转换执行时显示,但似乎在关闭后对 UIViewController 的任何调用都没有执行,或者至少没有显示。有什么建议吗?
    • 尝试将dismissModalViewControllerAnimated设置为NO,看看这是否是您的动画的时间问题。
    • 奇怪的是,这行得通。有没有办法展开然后执行secondaryView的后续动画?
    • 其实这并没有什么奇怪的。在我的原始答案中,解雇动画的“是”是复制和粘贴错误 - 它应该是“否”。我没有意识到你想要两个动画。我已经用另一种方法更新了我的答案,应该让你保留两个动画。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多