【问题标题】:UIAlertcontroller in custom TableViewCell WITH returned value自定义 UITableViewCell 中的 UIAlertcontroller WITH 返回值
【发布时间】:2016-01-05 07:10:30
【问题描述】:

我已经通过protocolsdelegates 搜索了如何在TableViewCell 中替换UIAlertController 的类似答案。我得到了那个部分的工作。但我在UIAlertController 中有两个动作。其中一个确认一个动作并返回true,另一个按钮是cancel并返回false。

我可以成功展示UIAlertController,但程序只是运行我的代码并忽略了我的确认按钮中的逻辑。我认为这是因为 delegate/protocol 方法在 parallel/asynchronously 中运行delegate 方法并且不等待返回值。

这是我的自定义 TableViewCell 中的代码:

var unattend: Bool!
let customAlert = UIAlertController(title: "Not going anymore?", message: "Event will disappear from this list, confirm?", preferredStyle: UIAlertControllerStyle.Alert)
let nevermindAction = UIAlertAction(title: "Nevermind", style: UIAlertActionStyle.Default) { (action: UIAlertAction) -> Void in
    unattend = false
}
let unattendAction = UIAlertAction(title: "Confirm unattending", style: UIAlertActionStyle.Destructive) { (action: UIAlertAction) -> Void in
    unattend = true
}
customAlert.addAction(nevermindAction)
customAlert.addAction(unattendAction)

self.delegate.goingCancelled(customAlert, unattend: unattend)) 

这是我的协议:

protocol GoingCancelledDelegate {
func goingCancelled(alert: UIAlertController) -> Bool
}

这是我在 TableViewController 中的代码,是的,我确实在类定义中输入了 GoingCancelledDelegate,只是没有在此处复制和粘贴:

func goingCancelled(alert: UIAlertController) -> Bool {
    presentViewController(alert, animated: true) { () -> Void in
    }
}

所以警报出现了,但我的UITableViewCell 代码不会等待返回值来处理其他逻辑。任何提示/建议/帮助将不胜感激!提前谢谢!

【问题讨论】:

    标签: ios swift2 xcode7 uialertcontroller


    【解决方案1】:

    这是因为您在没有确认操作的情况下调用self.delegate.goingCancelled(customAlert, unattend: unattend))

    var unattend: Bool!
     let customAlert = UIAlertController(title: "Not going anymore?", message: "Event will disappear from this list, confirm?", preferredStyle: UIAlertControllerStyle.Alert)
     let nevermindAction = UIAlertAction(title: "Nevermind", style: UIAlertActionStyle.Default) { (action: UIAlertAction) -> Void in
        unattend = false
    }
     let unattendAction = UIAlertAction(title: "Confirm unattending", style: UIAlertActionStyle.Destructive) { (action: UIAlertAction) -> Void in
        unattend = true
     self.delegate.goingCancelled(customAlert, unattend: unattend)) 
    }
    customAlert.addAction(nevermindAction)
    customAlert.addAction(unattendAction)
    

    【讨论】:

    • 对不起,我不太明白你的答案。我看到您将 self.delegate.goingCancelled() 函数移动到操作闭包中的 UIAlertAction() 语句中。但是该代码将在 customAlert.addAction() 函数之前运行,这不是问题吗?这些操作甚至还没有添加到我的 customAlert 中。
    • 它只会在您单击Confirm unattending 时运行,因为它的操作会调用该闭包。
    猜你喜欢
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多