我正在为 Objective C 中的 UIAlertView+Block 等回调实现实现 UIAlertView 扩展,但委托方法没有从此类调用任何帮助。提前致谢。
问问题
503 次
2 回答
0
根据苹果文档,来自警报视图的回调在后台线程中运行,接收它的代码可能在主线程中运行:
来自通知中心的 Apple iOS 文档:
在多线程应用程序中,通知总是在发布通知的线程中传递,这可能与观察者注册自己的线程不同。
您需要在主线程中回调通知,否则可能会发生奇怪的事情
dispatch_async(dispatch_get_main_queue(), {
//Call your notification from here
NSNotificationCenter.defaultCenter().postNotificationName(mySpecialNotificationKey, object: self)
})
于 2015-05-25T05:16:54.627 回答
0
这是解决问题后的更新答案。
typealias SelectBlock = (buttonIndex : Int) -> Void
typealias CancelBlock = () -> Void
private var blockSelect : SelectBlock?
private var blockCancel : CancelBlock?
extension UIAlertView {
//Initilization
func initWithTitle(
title : String ,
message : String,
onSelect : SelectBlock?,
onCancel : CancelBlock?,
otherButtonTitles : [String]){
//Initialize
self.title = title
self.delegate = self
self.message = message
//Block
if let onSelectObj = onSelect? {
blockSelect = onSelectObj
}
if let onCancelObj = onCancel? {
blockCancel = onCancelObj
}
//Other buttons
for buttons in otherButtonTitles {
self.addButtonWithTitle(buttons)
}
//Cancel index
// println("buttons \(self.numberOfButtons)")
self.cancelButtonIndex = 0
self.show()
}
func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
println("Index \(buttonIndex)")
if buttonIndex == alertView.cancelButtonIndex {
blockCancel!()
// println("Cancel")
}else{
blockSelect!(buttonIndex: buttonIndex)
// println("Retry")
}
}
}
于 2015-05-25T05:27:18.180 回答