問題描述
對此找不到清晰而翔實的解釋.
Couldn't find a clear and informative explanation for this.
推薦答案
在一個主題上搜索了一段時間后我沒有找到一個清晰的解釋,即使在它的類參考中UIAlertController 參考
After searching a while on a subject I didn't find a clear explanation , even in it's class reference UIAlertController Reference
沒關系,但對我來說不夠清楚.
It is ok, but not clear enough for me.
所以在收集了一些和平之后,我決定做出自己的解釋(希望對你有幫助)
So after collecting some peaces I decided to make my own explanation (Hope it helps)
就這樣吧:
UIAlertView
如所指出的那樣被棄用:Swift 中的 UIAlertViewUIAlertController
應該在 iOS8+ 中使用所以要先創建一個,我們需要實例化它,Constructor(init) 獲取 3 個參數:
UIAlertView
is deprecated as pointed out : UIAlertView in SwiftUIAlertController
should be used in iOS8+ so to create one first we need to instantiate it, the Constructor(init) gets 3 parameters:
2.1 title:String -> 顯示在警報對話框頂部的大粗體文本
2.1 title:String -> big-bold text to display on the top of alert's dialog box
2.2 message:String -> 較小的文本(幾乎可以解釋它的自身)
2.2 message:String -> smaller text (pretty much explains it's self)
2.3 prefferedStyle:UIAlertControllerStyle
-> 定義對話框樣式,大多數情況下:UIAlertControllerStyle.Alert
2.3 prefferedStyle:UIAlertControllerStyle
-> define the dialog box style, in most cases: UIAlertControllerStyle.Alert
現在要實際向用戶展示它,我們可以使用
showViewController
或presentViewController
并將我們的警報作為參數傳遞
Now to actually show it to the user, we can use
showViewController
orpresentViewController
and pass our alert as parameter
要添加一些與用戶的交互,我們可以使用:
To add some interaction with a user we can use:
4.1UIAlertController.addAction
創建按鈕
4.2UIAlertController.addTextField
創建文本字段
編輯說明:以下代碼示例,已針對 swift 3 語法進行了更新
Edit note: code examples below, updated for swift 3 syntax
示例 1:簡單對話框
@IBAction func alert1(sender: UIButton) {
//simple alert dialog
let alert=UIAlertController(title: "Alert 1", message: "One has won", preferredStyle: UIAlertControllerStyle.alert);
//show it
show(alert, sender: self);
}
示例 2:帶有一個輸入 textField 的對話框 &兩個按鈕
Example 2: Dialog with one input textField & two buttons
@IBAction func alert2(sender: UIButton) {
//Dialog with one input textField & two buttons
let alert=UIAlertController(title: "Alert 2", message: "Two will win too", preferredStyle: UIAlertControllerStyle.alert);
//default input textField (no configuration...)
alert.addTextField(configurationHandler: nil);
//no event handler (just close dialog box)
alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.cancel, handler: nil));
//event handler with closure
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
let fields = alert.textFields!;
print("Yes we can: "+fields[0].text!);
}));
present(alert, animated: true, completion: nil);
}
示例 3:一個自定義輸入 textField &一鍵
Example 3: One customized input textField & one button
@IBAction func alert3(sender: UIButton) {
// one input & one button
let alert=UIAlertController(title: "Alert 3", message: "Three will set me free", preferredStyle: UIAlertControllerStyle.alert);
//configured input textField
var field:UITextField?;// operator ? because it's been initialized later
alert.addTextField(configurationHandler:{(input:UITextField)in
input.placeholder="I am displayed, when there is no value ;-)";
input.clearButtonMode=UITextFieldViewMode.whileEditing;
field=input;//assign to outside variable(for later reference)
});
//alert3 yesHandler -> defined in the same scope with alert, and passed as event handler later
func yesHandler(actionTarget: UIAlertAction){
print("YES -> !!");
//print text from 'field' which refer to relevant input now
print(field!.text!);//operator ! because it's Optional here
}
//event handler with predefined function
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: yesHandler));
present(alert, animated: true, completion: nil);
}
希望有幫助,祝你好運 ;-)
這篇關于哪里可以找到關于 swift alert (UIAlertController) 的清晰解釋?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!