按钮标题更改后重置为默认值

时间:2015-06-18 17:21:42

标签: ios xcode swift uibutton uipickerview

更改按钮标题后,标题会根据pickerView正确更改。然后,当我点击应用程序中的其他页面并返回时,按钮标题将恢复为默认值当我尝试再次更改它时,只要弹出pickerView,它就会转到正确的新更改标题。为什么会发生这种情况,我该如何解决?

当用户在pickerView中选择一行时,我想要更改按钮标题的代码 -

  @IBAction func roleButton(sender: UIButton) {
    sender.setTitle(Manager.roleText, forState: UIControlState.Normal)
}

这里是pickerView代码 -

var Array = ["A", "B", "C", "D", "E"]

var Placement = 0

@IBOutlet var rolePicker: UIPickerView!

override func viewDidLoad() {
    super.viewDidLoad()

    rolePicker.delegate = self
    rolePicker.dataSource = self
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return Array[row]
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return Array.count
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

    return 1
}

@IBAction func Submit(sender: AnyObject) {
    if Placement == 0 {
        Manager.roleText = Array[0]
    }
    else if Placement == 1 {
        Manager.roleText = Array[1]
    }
    else if Placement == 2 {
        Manager.roleText = Array[2]
    }
    else if Placement == 3 {
        Manager.roleText = Array[3]
    }
    else if Placement == 4 {
        Manager.roleText = Array[4]
    }


}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    Placement = row
}

1 个答案:

答案 0 :(得分:0)

我相信,如果没有带有按钮的ViewController的引用,它就会被销毁,以便在你离开时立即释放内存。 (也就是说,一旦没有更多的引用)。如果您在segue.destinationViewController.delegate = self期间将ViewController1的引用传递给ViewController2(例如prepareForSegue),它应该保留它以便您可以返回它。