【问题标题】:dismiss current UIViewcontroller and present a new UiViewController关闭当前 UIViewcontroller 并呈现一个新的 UiViewController
【发布时间】:2016-06-04 06:16:16
【问题描述】:

我打算解散我当前的UIViewController 并提交给新的UIViewController

我使用了以下代码

 let newViewController: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
 self.presentViewController(newViewController, animated: false, completion: {
         self.dismissViewControllerAnimated(false, completion: nil)
    })

它给出了以下错误

2016-06-04 11:40:59.864 myApp[851:117649] 试图解除
演示控制器,同时已经过渡。 (<_uifullscreenpresentationcontroller:>) 2016-06-04 11:40:59.878 ePassBook[851:117649] transitionViewForCurrentTransition 未设置,演示控制器 在演示过程中被解雇? (<_uifullscreenpresentationcontroller:>)

【问题讨论】:

  • dismiss the action 点击​​关闭 UIViewController 然后自动 Present anotherViewController?

标签: ios objective-c swift uiviewcontroller segue


【解决方案1】:

使用此代码,

目标 C 代码:

[self.navigationController presentViewController:newViewController animated:NO completion:^{
    dispatch_after(0, dispatch_get_main_queue(), ^{
        [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    });
}];

Swift 代码:

self.navigationController?.presentViewController(newViewController, animated: false, completion: { () -> Void in
            dispatch_after(0, dispatch_get_main_queue(), { () -> Void in
                self.navigationController?.dismissViewControllerAnimated(false, completion: nil)

            })
        })

希望对你有帮助

【讨论】:

  • 您首先需要关闭当前提交的 VC,然后才能提交另一个。
【解决方案2】:

试试这个代码,我认为它很有帮助。

目标-C

[self.navigationController presentViewController:newViewController animated:NO completion:^{
    dispatch_async(dispatch_get_main_queue(), ^(void){
    //Run UI Updates

        [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    });
}];

斯威夫特

self.navigationController?.presentViewController(newViewController, animated: false, completion: { () -> Void in
           dispatch_async(dispatch_get_main_queue()) {
                self.navigationController?.dismissViewControllerAnimated(false, completion: nil)

        }
    })

【讨论】:

  • 感谢@Ravi 的回复..我试过你的代码..它没有工作........我实例化的控制器没有嵌入 UINavigationCONtroller..
【解决方案3】:

您首先需要关闭当前存在的viewcontroller,然后您只展示另一个,因为一次您不能展示两个viewcontrollers,所以您的代码应该是这样的,

  self.dismissViewControllerAnimated(false) { () -> Void in

        self.presentViewController(newViewController, animated: true, completion: nil)

    }

如果您使用的是 navigation controller,则在 navigation controller 上展示或关闭 VC

按照评论中的要求更新:

举个例子,

您有三个视图控制器,分别是 A、B 和 C,当前呈现的视图控制器是 C。比如 A -> B -> C

现在你想解除 C 并呈现新的 D,那么你必须创建 B 的实例,因为你正在解除 C,所以 self 没有任何意义。是零。

所以你应该创建 B 的实例让我们说 b 并在 b 上呈现 D

类似的,

 self.dismissViewControllerAnimated(false) { () -> Void in

        b.presentViewController(d, animated: true, completion: nil)

    }

如果您使用的是导航控制器,那么它应该是这样的,

   b.navigationController.presentViewCon.....

【讨论】:

  • 我尝试了以下代码.............它给了我以下错误:尝试在 > 其视图不在窗口层次结构中!
  • 那会发生什么?
  • 它关闭了当前的视图控制器但没有创建一个新的视图控制器............
  • 您需要在您提供的 VC 后面创建 VC 实例,然后在该 VC 的实例上展示您的视图。像 self.vc.presentView..... 一样,这里的 vc 是你的 VC 的实例,它在你首先解雇的 VC 后面
  • 一旦尝试在主线程上展示 newviewcontroller 如果它有效!
猜你喜欢
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多