在采取UIAlertAction后不要解雇

时间:2015-03-23 01:24:45

标签: ios swift uialertcontroller

所以我使用带有Action Sheet样式的UIAlertController来显示两个选项,一个用于取消操作,另一个用于删除数据。按钮工作正常,删除按钮工作,行动表解散。我的问题是,在后台从云端删除数据后,视图本身不会被忽略。为什么观点不解除自己?

@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)
}
}

1 个答案:

答案 0 :(得分:1)

此代码 -

// 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选项的处理程序中删除您的项目并关闭您的视图 -

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