Swift - 告警框(UIAlertView)的用法

1,下面代碼創建并彈出一個告警框,并帶有“取消”“確定”兩個按鈕
(注:自IOS8起,建議使用UIAlertController。點擊查看UIAlertController的用法)

class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()

    let alertView = UIAlertView()
    alertView.title = "系統提示"
    alertView.message = "您確定要離開hangge.com嗎?"
    alertView.addButtonWithTitle("取消")
    alertView.addButtonWithTitle("確定")
    alertView.cancelButtonIndex=0
    alertView.delegate=self;
    alertView.show()
}

func alertView(alertView:UIAlertView, clickedButtonAtIndex buttonIndex: Int){
    if(buttonIndex==alertView.cancelButtonIndex){
        print("點擊了取消")
    }
    else
    {
        print("點擊了確認")
    }
}

}

2,告警框有下面4種樣式
Default:默認樣式
PlainTextInput:帶輸入框的告警框
SecureTextInput:帶密碼框的告警框
LoginAndPasswordInput:帶輸入框和密碼框的告警框

下面是一個使用輸入框和密碼框的告警框樣例:
原文:Swift - 告警框(UIAlertView)的用法

import UIKit

class ViewController: UIViewController {

var alertView = UIAlertView()
 
override func viewDidLoad() {
    super.viewDidLoad()
     
    alertView.title = "系統登錄"
    alertView.message = "請輸入用戶名和密碼!"
    alertView.addButtonWithTitle("取消")
    alertView.addButtonWithTitle("確定")
    alertView.cancelButtonIndex=0
    alertView.delegate=self;
    alertView.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput
    alertView.show()
}
 
func alertView(alertView:UIAlertView, clickedButtonAtIndex buttonIndex: Int){
    if(buttonIndex==alertView.cancelButtonIndex){
        print("點擊了取消")
    }
    else
    {
        let name = alertView.textFieldAtIndex(0)
        let password = alertView.textFieldAtIndex(1)
        print("用戶名是:\(name!.text) 密碼是:\(password!.text)")
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

原文出自:www.hangge.com 轉載請保留原文鏈接:http://www.hangge.com/blog/cache/detail_537.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容