Writing handler for UIAlertAction

后端 未结 9 945
北恋
北恋 2020-11-28 05:10

I\'m presenting a UIAlertView to the user and I can\'t figure out how to write the handler. This is my attempt:

let alert = UIAlertController(ti         


        
9条回答
  •  盖世英雄少女心
    2020-11-28 05:19

    this is how i do it with xcode 7.3.1

    // create function
    func sayhi(){
      print("hello")
    }
    

    // create the button

    let sayinghi = UIAlertAction(title: "More", style: UIAlertActionStyle.Default, handler:  { action in
                self.sayhi()})
    

    // adding the button to the alert control

    myAlert.addAction(sayhi);
    

    // the whole code, this code will add 2 buttons

      @IBAction func sayhi(sender: AnyObject) {
            let myAlert = UIAlertController(title: "Alert", message:"sup", preferredStyle: UIAlertControllerStyle.Alert);
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:nil)
    
            let sayhi = UIAlertAction(title: "say hi", style: UIAlertActionStyle.Default, handler:  { action in
                self.sayhi()})
    
            // this action can add to more button
            myAlert.addAction(okAction);
            myAlert.addAction(sayhi);
    
            self.presentViewController(myAlert, animated: true, completion: nil)
        }
    
        func sayhi(){
            // move to tabbarcontroller
         print("hello")
        }
    

提交回复
热议问题