简体   繁体   中英

Why doesn't performSegueWithIdentifier work inside viewDidLoad?

I'm trying to trigger a storyboard segue as soon as viewDidLoad is called on a view controller. The segue has an identifier attached, and works fine when called from inside a method thats linked to a button or other control. But it doesn't work inside of viewDidLoad. It just silently fails. Is there some rule about segue's being ignored in viewDidLoad?

I've tried both this:

[self performSegueWithIdentifier: @"mySegue" 
                          sender: self];

and this:

[self dismissViewControllerAnimated:YES completion:^() {
[self performSegueWithIdentifier:@"mySegue" sender:self];
}];

Neither work.

You can not dismiss a view controller that isn't presented yet. didLoad has purely memory management functions, you can use it as (part of a) constructor. What may work, is to start a segue in viewDidAppear , however I would suggest to start with the view you want at the first time.

Most likely reason could be that the OS ignores second screen transition call while one is in progress. In your ViewDidLoad, the view transition (of the current view) is still not complete. You are asking another transition before it completes and the OS ignores it. It must be the reason that the segue works when called from a different function. Try calling inside ViewDidAppear (or after a time delay)

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