简体   繁体   中英

dismiss current UIViewcontroller and present a new UiViewController

I intend to dismiss my current UIViewController and present to a new 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
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>)

Use this Code,

Objective C Code:

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

Swift Code:

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

[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,

  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

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

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. It's nil.

So you should make instance of B lets say b and present D on that b

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.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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