Unable to pick up events with the specified delete in the closure

Asked 2 years ago, Updated 2 years ago, 31 views

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

2022-09-30 11:05

1 Answers

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)
    }
}


2022-09-30 11:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.