xcode 为什么 performSegueWithIdentifier 在 vi​​ewDidLoad 中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10308685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:05:01  来源:igfitidea点击:

Why doesn't performSegueWithIdentifier work inside viewDidLoad?

objective-ciosxcodeios5

提问by zakdances

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?

我试图在视图控制器上调用 viewDidLoad 时立即触发故事板 segue。segue 附加了一个标识符,并且在从链接到按钮或其他控件的方法内部调用时可以正常工作。但它在 viewDidLoad 中不起作用。它只是默默地失败了。是否有关于在 viewDidLoad 中忽略 segue 的一些规则?

I've tried both this:

我试过这两个:

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

and this:

和这个:

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

Neither work.

都不工作。

回答by Matthias

You can not dismiss a view controller that isn't presented yet. didLoadhas 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.

您不能关闭尚未呈现的视图控制器。didLoad具有纯粹的内存管理功能,您可以将其用作(a)构造函数的一部分。可行的方法是在 中开始一个segue viewDidAppear,但是我建议从您第一次想要的视图开始。

回答by zolio

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)

最可能的原因可能是操作系统在进行中时忽略了第二个屏幕转换调用。在您的 ViewDidLoad 中,(当前视图的)视图转换仍未完成。您在它完成之前要求另一个转换并且操作系统忽略它。这一定是从不同的函数调用时 segue 工作的原因。尝试在 ViewDidAppear 内部调用(或延迟一段时间后)