我有一个字符串类型变量多数民众赞成值通过PickerView更新,而当该值被更新时,然后更新按钮的标题,比如:如何使用swift监听变量中的值更改?
@IBOutlet weak var selectedCountry: UIButton!
var pickedCountry: String?
func checkForPickedCountry() {
if self.pickedCountry != nil {
selectedCountry.setTitle("\(pickedCountry!)", forState: .Normal);
} else {
selectedCountry.setTitle("Tap here pick a country", forState: .Normal);
}
UPDATE
现在就像我提到的那样,我使用pickerView来更新pickedCountry
的值,并且当从pickerView中选择一个国家时,它会显示第二个按钮,标题为selectedFlag
,它会拍摄另一个pickerView来选择国旗。现在我想听第一个pickedCountry
变化的原因是我可以将selectedFlag
按钮的标题更改为默认值。
因此,这里是最后的代码,我有:
@IBOutlet weak var selectedCountry: UIButton!
@IBOutlet weak var selectedFlag: UIButton!
var pickedCountry: String?
var pickedFlag: String?
func checkForPickedCountry() {
if self.pickedCountry != nil {
selectedCountry.setTitle("\(pickedCountry!)", forState: .Normal);
selectedFlag.hidden = false
} else {
selectedCountry.setTitle("Tap here pick a country", forState: .Normal);
selectedFlag.hidden = true
}
if self.pickedFlag != nil {
selectedFlag.setTitle("\(pickedCountry!)", forState: .Normal);
} else {
selectedFlag.setTitle("Tap here to pick a flag", forState: .Normal)
}
现在我该怎样设置selectedFlag
的标题为“点击这里picka标志时PickedCountry的`值改变??
如果我不想修改已经存在的函数,但只是在值更改时触发一个全新的函数,该怎么办?基本上说“如果pickCountry'的值改变了,改变uibutton蓝色的背景颜色 –
只要在willSet()中调用你的函数而不通过任何值 –
即时通讯对不起,但我想我的问题太大,因为你的方法kind've并没有帮助我的情况,因为我认为这是,请检查更新的问题.. –