All I am implementing a UIAlertView extension for callback implementation like UIAlertView+Block in Objective C but the delegate method is not calling from this class any help. Thanks in advance.
Asked
Active
Viewed 568 times
0
-
Can you post your relevant code here. – tounaobun May 25 '15 at 05:05
2 Answers
0
The callback from the alert view runs in a background thread and the code receiving it is probably running in the main thread, as per apple documentation:
From the Apple iOS Documentation on Notification Centers:
In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.
You need to call the notification back in the main thread otherwise strange things can happen
dispatch_async(dispatch_get_main_queue(), {
//Call your notification from here
NSNotificationCenter.defaultCenter().postNotificationName(mySpecialNotificationKey, object: self)
})

Icaro
- 14,585
- 6
- 60
- 75
0
This is the updated answer after solving the issue.
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")
}
}
}

Gopal Devra
- 564
- 2
- 5
- 24
-
1Your class is already a UIAlertView and you are creating another UIAlertView, you should change all the alertView for self. That should solve your problem – Icaro May 25 '15 at 05:50
-
@GopalDevra: This is not an *answer* to your question. Please add *edit* your question and add the code to it. – Martin R May 25 '15 at 08:01