准备搜身后,UiTextField不会改变

时间:2015-11-24 22:56:10

标签: xcode swift uitextfield segue

当我遇到下一个ViewController时,我遇到了一个奇怪的小问题。我设置它,以便当我segue它应该更改下一个ViewController的TextField但由于某种原因它不是。 segue有效,但TextField没有变化。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "Edit" {
        let cell = sender as! UITableViewCell
        let indexPath = TaskTableViews.indexPathForCell(cell)
        let itemController: TaskManager = segue.destinationViewController as! TaskManager
        let nItem: List = frc.objectAtIndexPath(indexPath!) as! List
        itemController.nItem = nItem
    }
    if (segue.identifier == "Give") {
        let itemController : TaskManager = segue.destinationViewController as! TaskManager
        itemController.addTaskText = "Great"

    }

}

继承我的secondViewController的ViewDidLoad ..

// View Did Load
    override func viewDidLoad() {
    super.viewDidLoad()

    if nItem != nil {
        addTask.text = (nItem?.taskName)!
        }

    addTask.placeholder = "Title ..."
    addTaskText = addTask.text!

1 个答案:

答案 0 :(得分:0)

检查您是否实际使用相关标识符执行segue并查看prepareForSegue。如果它像下面的代码应该工作。此外,在第二个ViewController中,您可以在viewDidLoad中的textField中设置文本。只需确保数据实际上是从第一个ViewController传递到第二个ViewController

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

    if segue.identifier == "Great" {

        let itemController = segue.destinationViewController as! YourDestinationViewController
        itemController.someStringVariable = "Great"
    }

}

在你的第二个ViewController中,你可以声明一个变量,它将在segue中获得它的值,然后使用该变量的值来设置textField的文本,如下所示:

var someStringVariable: String!

override func viewDidLoad() {
    super.viewDidLoad()

    //Check if the value has been set to your string and if it has set it to be the textField's text
    if someStringVariable != nil {

        textField.text = someStringVariable
    }
}