天天看點

Swift - 告警提示框(UIAlertController)的用法

自iOS8起,蘋果就建議告警框使用UIAlertController來代替UIAlertView和UIActionSheel。下面總結了一些常見的用法( 本文代碼都已更新至Swift3)

1,簡單的應用(同時按鈕響應Handler使用閉包函數)   

Swift - 告警提示框(UIAlertController)的用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

import

UIKit

class

ViewController

UIViewController

{

override

func

viewDidLoad() {

super

.viewDidLoad()

}

override

func

viewDidAppear(_ animated: 

Bool

){

super

.viewDidAppear(animated)

let

alertController = 

UIAlertController

(title: 

"系統提示"

,

message: 

"您确定要離開hangge.com嗎?"

, preferredStyle: .alert)

let

cancelAction = 

UIAlertAction

(title: 

"取消"

, style: .cancel, handler: 

nil

)

let

okAction = 

UIAlertAction

(title: 

"好的"

, style: .

default

, handler: {

action 

in

print

(

"點選了确定"

)

})

alertController.addAction(cancelAction)

alertController.addAction(okAction)

self

.present(alertController, animated: 

true

, completion: 

nil

)

}

override

func

didReceiveMemoryWarning() {

super

.didReceiveMemoryWarning()

}

}

2,除了彈出,還可以使用從底部向上滑出的樣式

(注意:如果上拉菜單中有“取消”按鈕的話,那麼它永遠都會出現在菜單的底部,不管添加的次序是如何)  

Swift - 告警提示框(UIAlertController)的用法
1 2 3 4 5 6 7 8 9

let

alertController = 

UIAlertController

(title: 

"儲存或删除資料"

, message: 

"删除資料将不可恢複"

,

preferredStyle: .actionSheet)

let

cancelAction = 

UIAlertAction

(title: 

"取消"

, style: .cancel, handler: 

nil

)

let

deleteAction = 

UIAlertAction

(title: 

"删除"

, style: .destructive, handler: 

nil

)

let

archiveAction = 

UIAlertAction

(title: 

"儲存"

, style: .

default

, handler: 

nil

)

alertController.addAction(cancelAction)

alertController.addAction(deleteAction)

alertController.addAction(archiveAction)

self

.present(alertController, animated: 

true

, completion: 

nil

)

3,按鈕使用“告警”樣式(文字顔色變紅,用來來警示使用者)   

Swift - 告警提示框(UIAlertController)的用法
1

let

okAction = 

UIAlertAction

(title: 

"好的"

, style: .destructive, handler: 

nil

)

4,添加任意數量文本輸入框(比如可以用來實作個登陸框)   

Swift - 告警提示框(UIAlertController)的用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

import

UIKit

class

ViewController

UIViewController

{

override

func

viewDidLoad() {

super

.viewDidLoad()

}

override

func

viewDidAppear(_ animated: 

Bool

){

super

.viewDidAppear(animated)

let

alertController = 

UIAlertController

(title: 

"系統登入"

,

message: 

"請輸入使用者名和密碼"

, preferredStyle: .alert)

alertController.addTextField {

(textField: 

UITextField

!) -> 

Void

in

textField.placeholder = 

"使用者名"

}

alertController.addTextField {

(textField: 

UITextField

!) -> 

Void

in

textField.placeholder = 

"密碼"

textField.isSecureTextEntry = 

true

}

let

cancelAction = 

UIAlertAction

(title: 

"取消"

, style: .cancel, handler: 

nil

)

let

okAction = 

UIAlertAction

(title: 

"好的"

, style: .

default

, handler: {

action 

in

//也可以用下标的形式擷取textField let login = alertController.textFields![0]

let

login = alertController.textFields!.first!

let

password = alertController.textFields!.last!

print

(

"使用者名:\(login.text) 密碼:\(password.text)"

)

})

alertController.addAction(cancelAction)

alertController.addAction(okAction)

self

.present(alertController, animated: 

true

, completion: 

nil

)

}

override

func

didReceiveMemoryWarning() {

super

.didReceiveMemoryWarning()

}

}

5,使用代碼移除提示框

1

self

.presentedViewController?.dismiss(animated: 

false

, completion: 

nil

)

6,提示框彈出後,過段時間自動移除

下面樣例彈出一個不帶按鈕的消息提示框,過個兩秒鐘提示框自動消失。

Swift - 告警提示框(UIAlertController)的用法
1 2 3 4 5 6 7 8

let

alertController = 

UIAlertController

(title: 

"儲存成功!"

,

message: 

nil

, preferredStyle: .alert)

//顯示提示框

self

.present(alertController, animated: 

true

, completion: 

nil

)

//兩秒鐘後自動消失

DispatchQueue

.main.asyncAfter(deadline: 

DispatchTime

.now() + 2) {

self

.presentedViewController?.dismiss(animated: 

false

, completion: 

nil

)

}

原文出自: www.hangge.com   轉載請保留原文連結: http://www.hangge.com/blog/cache/detail_651.html