1, Uialertcontroller Introduction:
※了解: Uialertcontroller-Box controller, used when you need to remind users to perform certain actions, usually in two styles: Intermediate Reminder box, bottom reminder box.
Apple Official Introduction: 2, Uialertcontroller use: 2-1, Uialertcontroller style:
2-2, Uialertcontroller use steps:
- Uialertcontroller use
alertControllerWithTitle:message:preferredStyle: Create Uialertcontroller controller, and Set Reminder box reminder message, title, and alert box style;
actionWithTitle:style:handler:create uialertaction by method;
- Alertcontroller call
addAction: method to add action to Alertcontroller;
- The source controller invokes the
presentViewController:animated:completion: method, showing the Alertcontroller as a modal form.
3, Uialertcontroller properties: 3-1, Uialertcontroller three major attributes:
※重要:The Preferredstyle property is a read-only property (that is: readOnly ).
3-2. Action property in Uialertcontroller:
※重要:Create a OK button, 一定要注意不能在提醒控制器的按钮的点击方法内部用到提醒控制器自己 you cannot put the following sentence inside the block; "Otherwise it will die loops, causing the warning controller to not be destroyed."
3-3. Add text Box Properties in Uialertcontroller:
※重要:Only the middle Reminder box style can add a text box, and the bottom reminder box cannot add a text box.
4, 2 Styles Example: 4-1, the middle reminder box:
Sample Program :
1.//MARK:-Create Uialertcontroller
2. Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@ "Warning" message:@ "Your account balance is not enough!" PreferredStyle: Uialertcontrollerstylealert];
3.
4.//MARK:-Set alert properties
5.//Alert.preferredstyle read-only attribute (readOnly)
6.//Alert.preferredstyle = Uialertcontrollerstylealert;
7.
8.//MARK:-Add text box
9. [Alert addtextfieldwithconfigurationhandler:^ (Uitextfield * _nonnull TextField) {
10.//1. Set placeholder text
Textfield.placeholder = @ "Please enter the recharge amount!";
12.}];
13.
//MARK:-Create button
Uialertaction *sure = [uialertaction actionwithtitle:@ "OK" style:uialertactionstyledestructive handler:^ ( Uialertaction * _nonnull action) {
16.//1. Get a text box with the Textfields property
NSString *text = [alert.textfields[0] text];
NSLog (@ "Your account has been credited:%@!", text);
19.}];
20.
Uialertaction *cancel = [uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstylecancel handler:^ ( Uialertaction * _nonnull action) {
NSLog (@ "Your account is down!");
23.}];
24.
//MARK:-Add action
[Alert Addaction:sure];
[Alert Addaction:cancel];
28.
//MARK:-Show Reminder Box
[Self Presentviewcontroller:alert animated:yes completion:^{
31.
32.}];
4-2, the bottom of the reminder box example:
Sample Program :
1.//Uialertcontroller The default is Uialertcontrollerstyleactionsheet style created by Alloc, Init
2. Uialertcontroller *actionsheet = [[Uialertcontroller alloc] init];
3.
4.//MARK:-Set properties
5. Actionsheet.title = @ "Warning";
6. Actionsheet.message = @ "Your balance is not enough, please recharge it in time!";
7.
8.//MARK:-Read-only property
9.//Actionsheet.preferredstyle = Uialertcontrollerstyleactionsheet;
10.
One.//MARK:-Create button
Uialertaction *sure = [uialertaction actionwithtitle:@ "OK" style:uialertactionstyledestructive handler:^ ( Uialertaction * _nonnull action) {
NSLog (@ "Your account is down!");
14.}];
15.
Uialertaction *cancel = [uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstylecancel handler:^ ( Uialertaction * _nonnull action) {
NSLog (@ "Your account has been paid!");
18.}];
19.
//MARK:-Add action
[Actionsheet Addaction:sure];
[Actionsheet Addaction:cancel];
23.
//MARK:-The bottom reminder box cannot add a text box
25.//[Actionsheet addtextfieldwithconfigurationhandler:^ (Uitextfield * _nonnull TextField) {
26.////1. Set placeholder text
27.//Textfield.placeholder = @ "Please enter the recharge amount!";
28.//}];
29.
30.
//MARK:-Display the Reminder box at the bottom
[Self Presentviewcontroller:actionsheet animated:yes completion:^{
33.
34.}];
iOS Core note--uialertcontroller