frodo1986
frodo1986

Reputation: 1

iOS swift prepareForSegue cannot pass data between two viewcontroller

This is my code of two viewcontroller, need pass "numberToDisplay" from class bookTwo to class bookThree, but it not work, always show 0 in bookThree. I am using swift3 on ios 10, xocod 8. bookTwo:

import UIKit
import Foundation
class bookTwo: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

 func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ms1"{
        let destinationVC = segue.destination as? bookThree;
        destinationVC?.numberToDisplay = 7

    }
  }
}

bookThree

import UIKit

class bookThree: UIViewController {
var numberToDisplay = 0

    @IBOutlet weak var showType: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
  showType.text = "Tapped \(numberToDisplay) times."
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Upvotes: 0

Views: 252

Answers (1)

KSR
KSR

Reputation: 1709

In Swift 3, the signature for method "prepareForSegue" has changed. So replace the code for "prepareForSegue" with this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ms1"{
            let destinationVC = segue.destination as? bookThree
            destinationVC?.numberToDisplay = 7

        }
    }

Upvotes: 1

Related Questions