2016年7月19日 星期二

[swift] UIAlertAction,UIAlertController,ActionSheet

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func showAlertView(sender: UIButton) {
        let alertController = UIAlertController(title: "會員登入", message: "請輸入帳號密碼", preferredStyle: .Alert)
        let button1 = UIAlertAction(title: "取消", style: .Default) { (button1) in
            
        }
        let button2 = UIAlertAction(title: "登入", style: .Default) { (button2) in
            let username = alertController.textFields?.first?.text
            let passwd = alertController.textFields?.last?.text
            if username == "class" && passwd == "a1234" {
                self.label1.text = "登入成功"
            }else{
                self.label1.text = "帳號密碼錯誤"
            }
        }
        
        alertController.addTextFieldWithConfigurationHandler { (textField) in
            textField.placeholder = "username"
        }
        
        alertController.addTextFieldWithConfigurationHandler { (textField) in
            textField.placeholder = "password"
            textField.secureTextEntry = true
        }
        
        alertController.addAction(button1)
        alertController.addAction(button2)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
    
    @IBAction func showActionSheet(sender: UIButton) {
        let alertController = UIAlertController(title: "Office課程費用查詢", message: "請選擇課程種類", preferredStyle: .ActionSheet)
        let button1 = UIAlertAction(title: "Word", style: .Default) { (button1) in
            self.label1.text = "Word:$7000"
        }
        let button2 = UIAlertAction(title: "Excel", style: .Default) { (button2) in
            self.label1.text = "Excel:$7900"
        }
        
        let button3 = UIAlertAction(title: "PowerPoint", style: .Default) { (button3) in
            self.label1.text = "PowerPoint:$8500"
        }
        let button4 = UIAlertAction(title: "Access", style: .Default) { (button4) in
            self.label1.text = "Access:$10000"
        }
        
        alertController.addAction(button1)
        alertController.addAction(button2)
        alertController.addAction(button3)
        alertController.addAction(button4)
        self.presentViewController(alertController, animated: true, completion: nil)
    }


}


showAlertView畫面





登入成功時的畫面






登入失敗時的畫面





showActionSheet的畫面







點選後的訊息畫面






沒有留言:

張貼留言