ios - 如何根据确认对话框防止SegmentedControl索引更改?

标签 ios swift xcode uisegmentedcontrol

我有一个 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/

相关文章:

IOS - 我想回收Viewcontroller上的服务器数据

ios - 应用旋转时自定义相机 View 覆盖尺寸问题

ios - 当用户触摸图像时切换图像

ios - Xcode:哪些文件需要成为我的目标的成员? (目标成员(member))

ios - tableView 中的 searchBar 查询(Swift)

ios - 让一个 View Controller 听另一个

ios - UIWebView LoadData 不接受 textEncodingName 和 baseURL 的 Nil 值

objective-c - IOS Sparrow 框架 : how to prevent double touch

ios - 根据 Swift 大小调整 UILabel 大小

ios - 在 iOS 8 中请求本地通知权限,但仍然有应用程序支持 iOS 7