将用户输入的文本传递给以下segue

时间:2015-09-27 22:25:53

标签: ios swift segue

我有一个带文本字段的屏幕。

当用户在该文本字段中输入文本并从文本字段中轻敲时,会发生segue。

以下屏幕的标签文字应更改为用户在之前输入的文字。

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var inputField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        inputField.delegate = self

        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
        view.addGestureRecognizer(tap)
    }

    func dismissKeyboard() {
        inputField.resignFirstResponder()
    }

    func textFieldDidEndEditing(inputField: UITextField) {
        let info = inputField.text
        print(info)
        performSegueWithIdentifier("goToBlue", sender: info)
    }

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

        if segue.identifier == "goToBlue" {
            if let blueVC = segue.destinationViewController as? BlueViewController {
                if let sentValue = sender as? String {
                    blueVC.receptacle = sentValue
                }
            }
        }
    }
}

print(info)语句不会打印任何内容

然而,segue有效,但标签没有显示任何内容。

class BlueViewController: UIViewController {

    @IBOutlet weak var blueText: UILabel!
    var receptacle = ""

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        blueText.text = receptacle
    }
}

显然这与时间安排有关......任何想法?

4 个答案:

答案 0 :(得分:0)

您的viewWillAppear函数首先运行,而您的插座变量未设置或您的标签未初始化。试试这个;

 @IBOutlet weak var blueText: UILabel! { didSet { blueText.text = receptacle } }

var receptacle: String? { didSet { blueText.text = receptacle } }

答案 1 :(得分:0)

更改

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    blueText.text = receptacle
}

override func viewDidLoad() {
    super.viewDidLoad()
    blueText.text = receptacle
}

答案 2 :(得分:0)

这是非常常见的方法(仅供参考,如果带有inputTextfield的vc是popover,你可以在呈现的vc上调用dismissViewController)

func textFieldDidEndEditing(inputField: UITextField) {
        textField.resignFirstResponder()
        let info = inputField.text
        print(info)
        performSegueWithIdentifier("goToBlue", sender: info)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "goToBlue" {
            if let blueVC = segue.destinationViewController as? BlueViewController {
                blueVC.receptacle = self.inputField.text
            }
        }
    }

答案 3 :(得分:0)

请勿使用sender传递您的值。 sender仅用于检查哪个元素已启动segue,但不能传递数据。只需将info作为类变量并传递值:

var info:String!

func textFieldDidEndEditing(inputField: UITextField) {
    info = inputField.text
    print(info)
    performSegueWithIdentifier("goToBlue", sender: info)
}

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

    if segue.identifier == "goToBlue" {
        if let blueVC = segue.destinationViewController as? BlueViewController {
             blueVC.receptacle = info
        }
    }
}