1. 程式人生 > >IOS初學-普通警告視窗的使用⚠️

IOS初學-普通警告視窗的使用⚠️

func test6() {
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
        button.setTitle("點選彈出警告⚠️", for: UIControlState())
        button.backgroundColor=UIColor.purple
        button.addTarget(self, action: #selector(ViewController.test6OnClick), for: UIControlEvents.touchUpInside);
        self.view.addSubview(button)
    }
    @objc func test6OnClick(_ button:UIButton){
        let dialog=UIAlertController(title: "Information", message: "Are you a student?", preferredStyle: UIAlertControllerStyle.alert);
        let yes=UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: {(Alerts:UIAlertAction) -> Void in print("Yes,I'm a student")})
        let no = UIAlertAction(title: "no", style: UIAlertActionStyle.default, handler: {(Alerts:UIAlertAction) -> Void in print("no,I'm not a student")})
        dialog.addAction(yes)
        dialog.addAction(no)
        
        self.present(dialog, animated: true, completion: nil);
        
    }