解散后弹出视图控制器。

3

我有一个导航控制器A,在A上推出视图控制器B。从B中,我模态呈现了视图控制器C。我需要同时解除C和从B到A的弹出操作。我希望这些操作按顺序进行,先进行解除动画,然后再进行从B到A的弹出动画。我尝试过以下代码,但没有成功:

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

有什么建议可以帮我实现这个目标吗?

检查导航控制器是否不为空...? - Adnan Aftab
我的按钮中的日志输出为“null” NSLog(@"%@", self.presentingViewController.navigationController); 不知道为什么。也许我需要创建一个对视图控制器的引用? - Diego
3个回答

1
如果您正在编写C视图控制器,则为:

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中。我尝试了您的代码,似乎不起作用。 - Diego
实际上,self.presentingViewController返回的是UINavigationController。请查看我的更新答案。 - Parag Shinde
@paragShinde 有没有什么好的理由来检查这个:[pvc isKindOfClass:[UINavigationController class]] - skypirate
呈现视图控制器可以是UIViewController或UINavigationViewController。 - Parag Shinde

1
我之前尝试过连续弹出两个窗口,但没有关闭一个再打开另一个。你可以尝试我的方法,看看是否适用于你。
在子视图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];
}

0
你在使用故事板和跳转吗?如果是的话,你可以使用退回跳转(unwind segue):
在你要回到的第一个视图控制器(而不是你要返回的那个视图控制器)中创建一个退回跳转动作:
- (IBAction)gotoMainMenu:(UIStoryboardSegue *)segue
{
    // if you need to do anything when you return to the main menu, do it here
}

然后在Storyboard中,您可以从“dismiss”按钮创建一个segue到场景上方的退出出口图标(enter image description here),您将在那里看到主菜单


抱歉,我应该明确一下,我正在使用XIB文件。 - Diego
明白了。我会将这个答案保留在这里,以防未来使用故事板的读者遇到这个问题。 - Rob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接