我有一個SegmentedControl。當用戶點擊它時,會出現一個確認對話框,詢問他們是否希望更改該值。如果他們點擊「取消」,我想取消對SegmentedControl值的更改。如何根據確認對話框防止SegmentedControl索引發生變化?
這是一個代碼段,我有:
@IBAction func indexChanged(_ sender: UISegmentedControl) {
let refreshAlert = UIAlertController(title: "Update", message: "Sure you wanna change this?", preferredStyle: UIAlertControllerStyle.alert)
refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
}))
refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
// Nothing
}))
present(refreshAlert, animated: true, completion: nil)
}
在此先感謝。
當然,任何代碼觀察(志願)這個segmentControl已經反應,你可以做驗證之前,但是。此外,由於您立即使用'self.segmentedControl.selectedSegmentIndex = lastSelectedIndex'重置所選索引,因此如果用戶以後選擇了OK,KVO觀察者將被觸發兩次,並可能第三次觸發。 – Yohst