简体   繁体   English

prepareForSegue 什么时候被调用?

[英]when does prepareForSegue get called?

So here is my situation, I have two view controllers: viewControllerA and tableViewControllerB.所以这是我的情况,我有两个视图控制器:viewControllerA 和 tableViewControllerB。 On my viewControllerA, I have a button.在我的 viewControllerA 上,我有一个按钮。 And when it gets pressed, it will call当它被按下时,它会调用

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

moreover, I wrote a prepareforSegue function that looks like the following:此外,我编写了一个 prepareforSegue 函数,如下所示:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "toControllerB"){

        var controllerB = segue.destinationViewController as tableViewControllerB
        controllerB.event = self.event
    }

And in my tableViewControllerB I have a function looks like the following在我的 tableViewControllerB 中,我有一个如下所示的函数

var event = nil
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    self.viewDidLoad()
    var cell = tableView.dequeueReusableCellWithIdentifier("controllerBCell") as? ControllerBCell ?? ControllerBCell()
    //some code
    print(event)
    return cell

}

And when I run my app, it seems like this tableview function gets called before the prepareforSegue function in ControllerA.当我运行我的应用程序时,这个 tableview 函数似乎在 ControllerA 中的 prepareforSegue 函数之前被调用。 And thus the "event" variable is nil.因此“事件”变量为零。 Therefore, I am wondering is it possible to call prepareforSegue function before this tableview function in controllerB?因此,我想知道是否可以在 controllerB 中的 tableview 函数之前调用 prepareforSegue 函数?

Thanks谢谢

The chain is something like this.链条是这样的。

  1. First prepareForSegue is called首先调用prepareForSegue
  2. Then viewDidLoad is called然后调用 viewDidLoad
  3. Then cellForRowAtIndexPath is called然后调用 cellForRowAtIndexPath

I guess the problem is your line我想问题是你的线路

self.viewDidLoad()

This function is called automatically.这个函数是自动调用的。 I just tried rest of the code at my end and it works fine.我只是在最后尝试了其余的代码,它工作正常。

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

相关问题 何时为嵌入式viewController调用prepareForSegue? - When is prepareForSegue called for embedded viewController? 未调用prepareForSegue - prepareForSegue is not called prepareForSegue在调用时未发送参数,导致崩溃 - prepareForSegue not sending parameters when called, resulting in a crash prepareForSegue在注册自定义单元格时不会被调用 - prepareForSegue not getting called when registering a custom cell iOS 6,Segue:第一次点击UITableViewCell时未调用prepareForSegue - iOS 6, Segue: prepareForSegue doesn't get called when tapping on UITableViewCell at first time 仅在添加第三个segue时,在didSelectRowAtIndexPath之前调用的prepareForSegue - prepareForSegue called before didSelectRowAtIndexPath only when third segue is added 当我在不使用performSegueWithIdentifier的情况下单击按钮时,不会调用prepareForSegue - prepareForSegue is not getting called when I click on a button without using performSegueWithIdentifier 从按钮的动作方法(Swift)调用PrepareForSegue时不起作用 - PrepareForSegue not working when called from button's action method (Swift) 为自定义UITableViewCell加载和注册笔尖时未调用prepareForSegue - prepareForSegue not called when load and register nib for custom UITableViewCell 何时调用cellForItemAtIndexPath? - When does cellForItemAtIndexPath get called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM