【问题标题】:View not dismissing after UIAlertAction taken采取 UIAlertAction 后视图不关闭
【发布时间】:2015-03-23 09:34:19
【问题描述】:

所以我使用带有 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)
}
}

【问题讨论】:

  • 我认为您应该使用 UIAlertControllerdismissViewControllerAnimated 函数在您删除数据时关闭。或者您发布自定义通知来处理这种情况。
  • 值得注意的是,对于这个问题的REALLY SLOPPY 'QUICK FIX'是:只需关闭动画。即,动画 == 错误。通常,这可以防止有问题的赛道状况;你也方便然后不必费心学习如何编程! (我喜欢那个:))

标签: ios swift uialertcontroller


【解决方案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)
})

【讨论】:

  • 这行得通。我早些时候尝试过这个,我在 Parse 框架中遇到了一些奇怪的错误,告诉我 BFTask 不能转换为 ()。又试了一次,这次成功了!谢谢!还有一个快速的问题,在同一个处理程序中,是否可以告诉超级视图 tableView 重新加载数据以反映删除?
  • 你可以发布通知,但通常你会通过 unwind segue 或viewWillAppear
  • 感谢您的建议,我认为 viewWillAppear 应该可以正常工作。再次感谢您的帮助!
猜你喜欢
  • 2016-06-07
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
相关资源
最近更新 更多