【问题标题】:Dismissviewcontroller and perform segueDismissviewcontroller 并执行 segue
【发布时间】:2012-12-05 22:01:47
【问题描述】:

我需要使用performSegueWithIdentifier 转到另一个视图控制器,但我还需要删除我当前所在的视图控制器。我该怎么做,我已经尝试过,但它不起作用:

[self performSegueWithIdentifier:@"next" sender:self];
[self dismissViewControllerAnimated:NO completion:nil];

//tried the other way, but it doesn't work either
[self dismissViewControllerAnimated:NO completion:nil];
[self performSegueWithIdentifier:@"next" sender:self];

似乎没有任何简单的方法可以做到这一点? 我有 4 个视图控制器,它们是这样连接的,我想从游戏结束到高分。

menu-->game----->gameover
menu-->highscore

【问题讨论】:

  • 您的视图布局是什么?您正在尝试解除模态呈现的 UIViewController 并执行去哪里的 segue?还有你说的不起作用是什么意思?它会崩溃吗?它会默默地失败吗?
  • 最终这听起来像是糟糕的设计,您使用的是模态还是推送segues?听起来您应该使用 push segues,然后您不必担心关闭视图控制器。
  • @Gabro 它说“警告:在演示或关闭过程中尝试从视图控制器关闭!”。它执行segue,但如果我回去,视图控制器仍然存在。所以它不会被解雇。
  • @mkral 我正在使用模态。我不能使用推送,因为它是一个游戏,所以它看起来不太好。所以基本上我有 4 个 vc:s 1 菜单,您可以从中选择播放或查看高分。然后当你玩我想去高分时,你会怎么做?菜单-->游戏-->游戏结束-->高分;菜单-->高分
  • 我仍然会使用 push 只是隐藏导航栏。然后你可以从高分弹出推送,特别是 popToRootViewController

标签: objective-c ios uiviewcontroller segue


【解决方案1】:

这个怎么样?

UIViewController *parentController = self.presentingViewController;
[picker dismissViewControllerAnimated:YES completion:^(void){
    [parentController performSegueWithIdentifier:@"next" sender:self];
}];

【讨论】:

  • 我之前测试过,但它不起作用,因为您在执行 segue 之前删除了视图控制器。但是它需要 viewcontroller 来执行 segue,所以 segue 永远不会被调用。
  • 我还没有测试过它,但它应该仍然能够执行 segue,因为它不会被删除。但是,如果您尝试过,那么它可能不起作用的另一个原因。
  • 好吧,我现在也无法测试它,因为我改变了,所以现在我使用导航控制器。但下次我会试试你的答案!
  • 为我工作。最后一行缺少右大括号。
  • 我用于基于导航控制器的应用程序。 [self.navigationController popViewControllerAnimated:YES];
【解决方案2】:

我遇到了同样的问题,如果有人来这里寻找答案,我就是这样做的。

//In viewController that is being dismissed
[self dismissViewControllerAnimated:NO completion:^{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"callSegue" object:self];
}];


//In viewController being presented
//in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(gotoNewView)
                                                 name: @"callSegue"
                                               object: nil];

-(void)gotoNewView {
    [self performSegueWithIdentifier:@"segueToPerform" sender:self];
}

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,直到我意识到我应该改用 push segue。
    新 VC 将立即解雇他们。

    【讨论】:

      【解决方案4】:

      更好:

      [self dismissViewControllerAnimated:NO completion:^(void)
      {
       // Perform the segue here.
      }];
      

      【讨论】:

        猜你喜欢
        • 2017-07-08
        • 2015-02-20
        • 1970-01-01
        • 1970-01-01
        • 2012-08-18
        • 1970-01-01
        • 2017-12-06
        • 2018-11-08
        • 1970-01-01
        相关资源
        最近更新 更多