ios - 采取 UIAlertAction 后 View 未关闭

标签 ios swift uialertcontroller

所以我使用带有 Action Sheet 样式的 UIAlertController 来显示两个选项,一个用于取消操作,另一个用于删除数据。按钮工作正常,删除按钮工作,操作表关闭。我的问题是,在后台从云中删除数据后, View 本身不会消失。为什么 View 不自行解散?

@IBAction func deleteRecipePressed() {
// Create the alert controller
let actionSheetController: UIAlertController = UIAlertController(title: "Delete Recipe?", message: "Are you positive you want to delete this recipe? This action can not be undone!", preferredStyle: .ActionSheet)

// Create and add the cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {action -> Void in
  // Just dismiss the action sheet
})
actionSheetController.addAction(cancelAction)

// Create and add the delete action
let deleteAction: UIAlertAction = UIAlertAction(title: "Delete", style: .Default, handler: {action -> Void in
  // Set deleteRecipe to true to flag for deletion
  self.deleteRecipe = true
})
actionSheetController.addAction(deleteAction)

// Present the alert
self.presentViewController(actionSheetController, animated: true, completion: nil)

// If flagged for deletion, delete the recipe and dismiss the view
if (deleteRecipe == true) {
  recipeObject.deleteInBackground()
  self.dismissViewControllerAnimated(true, completion: nil)
}
}

最佳答案

这段代码-

// If flagged for deletion, delete the recipe and dismiss the view
if (deleteRecipe == true) {
  recipeObject.deleteInBackground()
  self.dismissViewControllerAnimated(true, completion: nil)
}

显示操作表后立即执行。即在用户选择任一选项之前。 self.presentViewController(actionSheetController,animated: true,completion: nil) 不会阻塞当前线程。

您需要删除您的项目并在 delete 选项的处理程序中关闭您的 View -

let deleteAction: UIAlertAction = UIAlertAction(title: "Delete", style: .Default, handler: {action -> Void in
  recipeObject.deleteInBackground()
  self.dismissViewControllerAnimated(true, completion: nil)
})

关于ios - 采取 UIAlertAction 后 View 未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29201975/

相关文章:

ios - 由于 SFSafariViewController,YSLSearchViewController 无法在 iOS 9 上工作

ios - 无法将 String 类型的值分配给 UILabel 类型

swift - Swift 中的 JSONSerialization.jsonObject 性能

ios - 从iOS主屏幕启动的React Web App怪异行为

ios - RoboVM Studio - LibGDX - UnsatisfiedLinkError - IOS 模拟器不工作?

ios - 如何让自定义导航 Controller 为每个 Storyboard提供相同的自定义导航栏按钮

ios - Swift 2,无法识别的选择器发送到 UIAlertController 的实例

ios - 如何在 UIAlertAction 中显示选定的操作?

ios - 升级到 iOS 11.3 破坏了 __weak UIAlertAction

iOS Core 蓝牙在蓝牙设备连接时通知应用程序