prepareForSegue and PerformSegueWithIdentifier sender

后端 未结 6 911
执笔经年
执笔经年 2021-02-03 13:18

I am wondering about how the functions in the title work and also about the sender parameter.

Lets say a button click calls the performSegue method, does that also call

6条回答
  •  臣服心动
    2021-02-03 14:00

    The performSegue method calls a segue to be performed from one view to another. Before the segue actually takes place, the prepareForSegue method is called, and if you want to pass data between the views, you'd do it there.

    The performSegue method doesn't take the parameter you want to send. It's only used to call the segue in the first place. Any data that you want to send will be done through prepareForSegue.

    Here's an example.

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Do any additional setup after loading the view.
    
        performSegueWithIdentifier("test", sender: self)
        //You can set the identifier in the storyboard, by clicking on the segue
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "test"{
            var vc = segue.destinationViewController as! RandomViewController
            vc.data = "Data you want to pass"
            //Data has to be a variable name in your RandomViewController
        }
    }
    

    Let me know if this helps!

提交回复
热议问题