【问题标题】:Pop view controller after dismiss关闭后弹出视图控制器
【发布时间】:2014-10-21 14:22:39
【问题描述】:

我有一个导航控制器 A,我在其上推送视图控制器 B。我从 B 模态地呈现视图控制器 C。我需要同时关闭 C 和弹出 B。我想按顺序进行,首先保持关闭动画,然后是从 B 到 A 的弹出动画。我尝试了这段代码但没有成功:

[self dismissViewControllerAnimated:YES completion:^{
       [self.presentingViewController.navigationController popViewControllerAnimated:YES];
}];

关于如何实现这一点的任何建议?

【问题讨论】:

  • 检查导航控制器是否不为零...?
  • 这个登录我的按钮给出“null”NSLog(@"%@", self.presentingViewController.navigationController); 不明白为什么。我可能需要创建对视图控制器的引用?

标签: ios objective-c popviewcontrolleranimated


【解决方案1】:

如果您使用 C viewcontoller 编写,那么:

UIViewController *pvc = self.presentingViewController;
UINavigationController *navController = [pvc isKindOfClass:[UINavigationController class]] ? (UINavigationController *)pvc : pvc.navigationController;
[self dismissViewControllerAnimated:YES completion:^{
  [navController popViewControllerAnimated:YES];
}];

或者如果在B视图控制器中

[self.presentedViewController dismissViewControllerAnimated:YES completion:^{
   [self.navigationController popViewControllerAnimated:YES];
}];

【讨论】:

  • 按钮位于C中。我试过你的代码,似乎不起作用。
  • 其实self.presentingViewController返回UINavigationController。请参阅我的更新答案。
  • @paragShinde 我们检查这个的任何充分理由:[pvc isKindOfClass:[UINavigationController class]]
  • 当前视图控制器可以是 UIViewController 或 UINavigationViewController。
【解决方案2】:

我之前曾尝试连续弹出两次,但没有关闭一次并弹出另一次。你可以试试我做的,看看它是否适合你。

在子视图 B 中:

- (void)subViewCController:(SubViewCController *)controller didSelectID:(NSNumber *)theID
{
    // do something with theID...
    // for my case, I popped
    // [self.navigationController popViewControllerAnimated:YES];

    // for your case
    [self dismissViewControllerAnimated:YES completion:nil];

    // Somehow, adding a small delay works for me and the animation is just nice
    [self performSelector:@selector(backToSubViewA) withObject:nil afterDelay:0.6];
}

- (void)backToSubViewA
{
    [self.navigationController popViewControllerAnimated:YES];
}

【讨论】:

    【解决方案3】:

    您是否使用故事板和转场?如果是这样,您可以使用 unwind segue:

    在您的第一个视图控制器(您要跳回的那个,而不是您要返回的那个)中,创建一个展开转场动作:

    - (IBAction)gotoMainMenu:(UIStoryboardSegue *)segue
    {
        // if you need to do anything when you return to the main menu, do it here
    }
    

    然后在情节提要中,您可以在场景上方的栏中创建从“关闭”按钮到出口出口图标 () 的转场,您会看到main menu 列在那里。

    【讨论】:

    • 对不起,我应该指定,我正在使用 xib 文件。
    • 明白。我将在此处保留此答案,以防使用故事板的未来读者偶然发现此问题。
    猜你喜欢
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    相关资源
    最近更新 更多