多个文本字段无法更改第二个视图控制器中的标签

时间:2018-11-20 14:32:48

标签: xcode segue textfield

我现在已经在第二个视图控制器中更改了标签,具体取决于我在第一个视图控制器中按下的按钮,但是当我尝试添加另外两个按钮和标签时​​,第二个视图控制器无法正确更新。如果我单击buttonOne,则第二个视图控制器中的标签会正确更新,但是label1中已经有文本,而本来不应该。

ViewController2

第二个ViewController的模拟器

enter image description here

第一个ViewController的模拟器

enter image description here

ViewController 1:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField2: UITextField!
    @IBOutlet weak var textField3: UITextField!


    var isFirstButtonClicked: Bool = false
    var isSecondButtonClicked: Bool = false

    @IBAction func buttonOne(_ sender: Any) {
        if textField.text != "" {
            isFirstButtonClicked = true
            performSegue(withIdentifier: "segue", sender: self)
        }
    }

    @IBAction func buttonTwo(_ sender: Any) {
        if textField1.text != "" {
            isFirstButtonClicked = false
            performSegue(withIdentifier: "segue", sender: self)
        }
    }


    @IBAction func buttonThree(_ sender: Any) {
        if textField2.text != "" {
            isSecondButtonClicked = true
            performSegue(withIdentifier: "segue", sender: self)
        }
    }

    @IBAction func buttonFour(_ sender: Any) {
        if textField3.text != "" {
            isSecondButtonClicked = false
            performSegue(withIdentifier: "segue", sender: self)
        }
    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        var secondController = segue.destination as! SecondViewController
        if isSecondButtonClicked {
            secondController.myString1 = textField2.text!
        }
        else {
            secondController.myString1 = textField3.text!
        }

        if isFirstButtonClicked {
            secondController.myString = textField.text!
        }
        else {
            secondController.myString = textField1.text!
        }
    }
}

ViewController 2:

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var label1: UILabel!

    var myString = String()
    var myString1 = String()


    @IBAction func shareButton(_ sender: Any) {

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        label.text = myString
        label1.text = myString1
    }
}

0 个答案:

没有答案