我有一个 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)
}
提前致谢。
最佳答案
最好的方法是保留一个保存最后选定索引的变量。在 cancel 的完成处理程序上,将段的选定索引设置为变量的值。在 Ok 的完成处理程序中,使用当前选定的索引更新变量。
关于ios - 如何根据确认对话框防止SegmentedControl索引更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39831957/