8. alertsviewcontroller class 8.1 uialertview
This section describes two common methods to obtain the user's choice. One is to use the class uiactionsheet and the other is the class uialertview.
These two classes are very convenient and simple to use.
For alert view, I only analyze one
Uialertview * Alert = [uialertview
Alloc] initwithtitle: @ "uialertview"
Message: @ "enter a password :"
Delegate: Self
Cancelbuttontitle: @ "cancel"
Otherbuttontitles: @ "OK ",
Nil];
Generate an alert view. If you are concerned about your selection, set delegate.
Alertview :( uialertview *) actionsheet clickedbuttonatindex :( nsinteger) buttonindex
The first parameter is the alert view pointer, because you may display multiple or multiple Alert Views in A View Controller. The second parameter is the user's choice, there is no way to obtain the title of the button selected here, but the title is added by yourself with the code, so we don't actually care about the title, the index setting above hi. Cancel button is 0, followed by other button is 1, 2 ....
Alert. alertviewstyle =
Uialertviewstylesecuretextinput;
The above function is added in IOS 5.0. There are four methods,
Uialertviewstyledefault = 0,
Uialertviewstylesecuretextinput,
Uialertviewstyleplaintextinput,
Uialertviewstyleloginandpasswordinput
The first one is the default one, that is, there is no input box, the second is a password input box, the third is a plaintext input box, and the fourth is both. In alertview :( uialertview *) actionsheet clickedbuttonatindex :( nsinteger) buttonindex, we can use the code
[Actionsheet
Textfieldatindex: 0]. Text
Obtain the response text.
[Alert
Show];
[Alert
Release];
In the above two sentences, we need to pay attention to memory management. The alert variable has a memory when alloc, and the counter is 1, then, when the show function is called, the counter will be implicitly added with 1. In this way, the memory counter pointed by alert is still 1 in the last sentence. Here the rule is met. You need to add one, you must subtract one. If it is not added, you are not responsible. If the function show adds one to the memory where alert is located, it will subtract one when the dialog box disappears.
8.2 uiactionsheet
For this class, I just analyzed a piece of code:
// Open a dialog with two custom buttons
Uiactionsheet * actionsheet = [[uiactionsheet
Alloc] initwithtitle: @ "uiactionsheet <title>"
Delegate: Self
Cancelbuttontitle: Nil
Destructivebuttontitle: Nil
Otherbuttontitles: @ "button1 ",
@ "Button2", nil];
Here, both the cancel button and destructive button can be nil or not nil, or nil. The other is not, but we need to know the order of the button index. If there is only one, so this must be 0. If there is no one, then the sequence starts in the following other button. For example, button1 is 0 and button2 is 1. If both of them have, note that, unlike alertview, destructive button is 0, cancel button is 1, and other button starts from 2.
Actionsheet. actionsheetstyle =
Uiactionsheetstyledefault;
Actionsheet. destructivebuttonindex = 1;
// Make the second button red (destructive)
Specify the button to display in red. By default, if both cancel and destructive are available, destructive is red. If there is only one or none, no button is displayed in red. It must be specified by yourself.
[Actionsheet
Showinview: Self. View];
// Show from our table view (pops up in the middle of the table)
[Actionsheet
Release];