简体   繁体   English

performSegueWithIdentifier在其他ViewController中不起作用

[英]performSegueWithIdentifier doesn't work in a different ViewController

I have the following storyboard: Main Storyboard 我有以下情节提要主情节提要

In it, several custom View Controllers are programmatically embedded in the Scroll View. 其中,几个自定义View Controller以编程方式嵌入到Scroll View中。 In one of them, a button is present and should trigger a segue to show the "hey" screen. 在其中一个按钮中,有一个按钮,应该触发一个按钮以显示“嘿”屏幕。

I have then wrote the following code: 然后,我编写了以下代码:

@IBAction func addNewDateButtonDidTouched(sender :AnyObject) {
    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let storyboardInit = mainStoryboard.instantiateViewControllerWithIdentifier("mainview")
    storyboardInit.performSegueWithIdentifier("showNewDate", sender: self)
}

This @IBAction seems to reload the inital view controller and perform the segue correclty (Xcode doesn't return any error). 这个@IBAction似乎重新加载了初始视图控制器并执行了segue修正(Xcode不返回任何错误)。 But the "hey" screen doesn't show up and its viewDidLoad() doesn't load. 但是“嘿”屏幕未显示,并且其viewDidLoad()也未加载。

Any hint? 有什么提示吗?

Instead of calling 而不是打电话

storyboardInit.performSegueWithIdentifier("showNewDate", sender: self)

try 尝试

self.performSegueWithIdentifier("showNewDate", sender: self)

The storyboardInit in your code will instantiate a UIViewController that has an identifier " mainview ". 您代码中的storyboardInit将实例化一个具有标识符“ mainview ”的UIViewController But this UIViewController hasn't been added to the screen, when you're asking the controller to perform a segue. 但是,当您要求控制器执行检测时,此UIViewController尚未添加到屏幕上。

So, I wouldn't do that. 所以,我不会那样做。 What I would do, to segue to the hey screen is either : 我想做的就是显示以下内容:

  1. Create a segue from the current controller and just do this in the code : self.performSegueWithIdentifier 从当前控制器创建self.performSegueWithIdentifier ,只需在代码中执行以下操作: self.performSegueWithIdentifier
  2. Instantiate the hey controller like this : instantiateViewControllerWithIdentifier , and then just do the self.presentViewController 实例化哎这样的控制器: instantiateViewControllerWithIdentifier ,然后就做self.presentViewController

So, to transition to a new controller, you have to start from the current controller. 因此,要过渡到新控制器,您必须从当前控制器开始。

if i understand your viewcontroller hierarchy correctly... 如果我正确理解您的ViewController层次结构...

since the viewcontroller that contains the button to trigger the segue is a childviewcontroller of the viewcontroller that has the segue setup in storyboard i think you have to call presentingViewController?.performSegueWithIdentifier("showNewDate", sender: self) . 因为包含触发segue的按钮的viewcontroller是在故事板中具有segue设置的viewcontroller的childviewcontroller,所以我认为您必须调用presentingViewController?.performSegueWithIdentifier("showNewDate", sender: self)

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

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