【问题标题】:How to unwind segue with alert view in Swift?如何在 Swift 中使用警报视图解除 segue?
【发布时间】:2015-04-30 05:22:24
【问题描述】:

我有几个视图控制器。如果确认警报视图,我需要返回第一个视图控制器。如果没有放松的 Segue,我会这样做:

@IBAction func unwindToDelete ( segue: UIStoryboardSegue ) {

    let alertView = UIAlertController(title: "Delete?", message: "Are you sure you wante to delete?", preferredStyle: .ActionSheet)

    let deleteAction = UIAlertAction (title: "Delete", style: .Destructive ) { alertAction in

        self.deleteChoice = true
    }

    let cancelAction = UIAlertAction (title: "Cancel", style: .Cancel ) {  alertAction in

    }

    alertView.addAction(deleteAction)
    alertView.addAction(cancelAction)

    self.presentViewController(alertView, animated: true, completion: nil)

}

但是如果我这样做,在这段代码中它会因为最后一行代码而崩溃。

这是错误:

2015-04-30 14:59:45.605 PhotosCollection[4624:182995] 
popToViewController:transition: called on <UINavigationController 0x7a67aeb0> 
while an existing transition or presentation is occurring; the navigation 
stack  will not be updated. 

如何在解除 segue 的同时完成警报视图。

谢谢

【问题讨论】:

  • 看起来你正在调用 unwind segue 而呈现动画是适当的...在设置一定的延迟后尝试 unwind 它...
  • 您是否尝试在按钮上添加处理程序,以便在拦截“删除”事件然后展开?

标签: iphone swift uialertview unwind-segue


【解决方案1】:

您在放松发生后发出警报。如果用户选择取消删除,您希望根本不执行展开。我建议如下:

  1. 不要将展开 segue 连接到删除按钮,而是将其连接到视图控制器顶部的视图控制器图标,以便您可以通过编程方式调用展开。这个答案向您展示了如何做到这一点: Setting up the unwind segue。 为展开转场指定一个标识符,例如 "doUnwind"
  2. 在删除按钮的@IBAction 中,提出警告,询问用户是否真的要删除。

  3. 在删除按钮的处理程序中,以编程方式调用展开转场。

        @IBAction func deleteButton (button: UIButton) {
    
            let alertView = UIAlertController(title: "Delete?", message: "Are you sure you wante to delete?", preferredStyle: .ActionSheet)
    
            let deleteAction = UIAlertAction (title: "Delete", style: .Destructive ) { alertAction in
    
                 self.performSegueWithIdentifier("doUnwind", sender: self)
            }
    
            let cancelAction = UIAlertAction (title: "Cancel", style: .Cancel ) {  alertAction in
    
            }
    
            alertView.addAction(deleteAction)
            alertView.addAction(cancelAction)
    
            self.presentViewController(alertView, animated: true, completion: nil)
    
        }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    相关资源
    最近更新 更多