I want to alert after hitting the API, pick up the event, and then move on to the next event, but delegate:self
in the closure does not pick up the event.
I haven't touched Objective-C and I'm a beginner at Swift, so I'm having trouble finding a solution.
typealias CompletionHandler=(result:Bool?) - > Void
class hogeViewController:UIViewController, UIAlertViewDelegate{
var completion —CompletionHandler?
functest(completion:(result:Bool)->Void){
let request = NSURLRequest (URL: NSURL (string: apiUrl)!)
NSURLConnection.sendAsynchronousRequest (request, queue:NSOperationQueue.mainQueue(), completionHandler: {(response:NSURLResponse!, data:NSData!, error:NSError!) in
let alert=UIAlertView(
message —testMessage
delete —self,
cancelButtonTitle:nil,
otherButtonTitle: "test1", "test2")
alert.show()
return
})
}
US>funcalertView(alertView:UIalertView, clickedButtonAtIndexButtonIndex:Int){
println("clickedButtonAtIndex")
println (buttonIndex)
self.completion!(result:true)
}
}
Please let me know if you know anything about it.
swift
I fixed the details because the compilation didn't go through, but in my environment, the UIAlertView button event was picked up properly.
Environment: iPhone 6 (iOS 8.1.3)
By the way, since UIAlertView is deprecated from iOS 8, it is recommended that you use the UIAlertController instead of iOS8 or higher.
typealias CompletionHandler=(result:Bool?) - > Void
classViewController:UIViewController, UIAlertViewDelegate{
let apiUrl="https://www.google.co.jp"
var completion:CompletionHandler?={(result:Bool?)in
println("API completed!\(result)")
}
functest(){
let request = NSURLRequest (URL: NSURL (string: apiUrl)!)
NSURLConnection.sendAsynchronousRequest (request, queue:NSOperationQueue.mainQueue(), completionHandler: {(response:NSURLResponse!, data:NSData!, error:NSError!) in
let alert=UIAlertView(
title: "Alert Title",
message: "Alert message",
delete —self,
cancelButtonTitle:nil,
otherButtonTitle: "test1", "test2")
alert.show()
return
})
}
US>funcalertView(alertView:UIalertView, clickedButtonAtIndexButtonIndex:Int){
println("clickedButtonAtIndex")
println (buttonIndex)
self.completion?(result:true)
}
}
© 2024 OneMinuteCode. All rights reserved.