简体   繁体   English

关闭当前的UIViewcontroller并呈现一个新的UiViewController

[英]dismiss current UIViewcontroller and present a new UiViewController

I intend to dismiss my current UIViewController and present to a new UIViewController . 我打算关闭我当前的UIViewController并呈现给一个新的UIViewController

I used the following code 我使用了以下代码

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

It gave the following error 它给出了以下错误

2016-06-04 11:40:59.864 myApp[851:117649] Trying to dismiss the 2016-06-04 11:40:59.864 myApp [851:117649]试图解雇
presentation controller while transitioning already. 演示控制器已经过渡。 (<_UIFullscreenPresentationController: 0x1703e6900>) 2016-06-04 11:40:59.878 ePassBook[851:117649] transitionViewForCurrentTransition is not set, presentation controller was dismissed during the presentation? (<_UIFullscreenPresentationController:0x1703e6900>)2016-06-04 11:40:59.878 ePassBook [851:117649]没有设置transitionViewForCurrentTransition,演示控制器在演示期间被解雇了? (<_UIFullscreenPresentationController: 0x1703e6900>) (<_UIFullscreenPresentationController:0x1703e6900>)

Use this Code, 使用此代码,

Objective C Code: 目标C代码:

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

Swift Code: 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)

            })
        })

hope its helpful 希望它有所帮助

Try to this code I think it's Helpfully. 尝试这个代码我觉得它很有帮助。

OBJECTIVE-C Objective-C的

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

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

SWIFT 迅速

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

        }
    })

You first need to dismiss currently present viewcontroller then after you only present another because at a time you can't present two viewcontrollers so, your code should be like, 你首先需要解雇当前存在的viewcontroller然后你只呈现另一个,因为你一次不能呈现两个viewcontrollers所以,你的代码应该像,

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

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

    }

If you are using navigation controller then present or dismiss VC on navigation controller 如果您使用navigation controller则在navigation controller上显示或关闭VC

Update as asked in comment: 按评论中的要求更新:

Take an example, 举个例子,

You have three View controllers say A,B and C and currently presented viewController is C. like A - > B - > C 你有三个View控制器说A,B和C,目前提供的viewController是C,如A - > B - > C.

Now you want to dismiss C and present new D then you must make instance of B because you are dismissing C, so self means nothing. 现在你要解雇C并提出新的D然后你必须做出B的实例,因为你正在解雇C,所以自我意味着什么。 It's nil. 这是零。

So you should make instance of B lets say b and present D on that b 所以你应该让B的实例说b并在那个b上呈现D.

Something like, 就像是,

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

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

    }

If you are using navigation controller then it should be like, 如果您使用的是导航控制器,那应该是,

   b.navigationController.presentViewCon.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM