king yau
king yau

Reputation: 510

Swift 2 IBAction and Segue

How to print 1111111 => 22222 => 333333333 of below code? Can anyone tell me why the IBAction is not the first, but prepareForSegue is?

A.swift

 @IBAction func ButtonTOClassB(sender: AnyObject) {
        print("1111111")
    }



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showingB" {
       print("22222")
}

B.swift

 override func viewDidLoad() {
        super.viewDidLoad()

        print("333333333")
}

Console:

22222
1111111
333333333

Upvotes: 1

Views: 246

Answers (2)

Tapani
Tapani

Reputation: 3231

Interface Builder adds a segue action to the button with addTarget. Then your action is added after that. As the actions are called in the same order as they were added, prepareForSegue gets called first. If you wish your action to be called first you should perform the segue programmatically as RDC instructs or just not use a segue, but push the view controller programmatically.

@IBAction func showLocation(sender: AnyObject) {
    let storyboard = UIStoryboard(name: "MyStoryBoard", bundle: nil)
    let bvc = storyboard.instantiateViewControllerWithIdentifier("ViewControllerB") as! B

    self.navigationController?.pushViewController(bvc, animated: true)
}

Upvotes: 2

swiftBoy
swiftBoy

Reputation: 35783

In your IBAction method perform segue programmatically

@IBAction func showLocation(sender: AnyObject) 
{

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

   print("1111111")
}

In Storyboard you need to define custom segue name = segue_name_here

enter image description here

Upvotes: 2

Related Questions