Original: http://my.oschina.net/plumsoft/blog/42763
The action sheet in the iOS program is like the OK-Cancel dialog box in Windows to force the user to make a selection. When a user is about to take a risk, the action sheet is often used to prompt the user for danger, so that the user has the opportunity to cancel the operation.
Alert is equivalent to the MessageBox in Windows and is similar to action sheet. The difference is that alert can have only one selection, and action sheet has at least two options.
As always, suppose we have created a single View application and opened the Viewcontroller.xib file.
First, we put a button on the view, we want to effect is: Click button to open an action Sheet, then click on the action Sheet a button, pop up an alert.
1, first of all, to add code in the ViewController.h, so that it implements a protocol. Add the code where the <uiactionsheetdelegate> is added at the end of the @interface line, and after that the line of code is:
@interface viewcontroller:uiviewcontroller<uiactionsheetdelegate>
2. Drag and drop a button onto the view to change the button name to do something.
3. Create an action map for this button, map to ViewController.h, event type default, name buttonpressed.
4. Find the Buttonpressed method in VIEWCONTROLLER.M and add the following code:
-(Ibaction) buttonpressed: (ID) Sender { Uiactionsheet *actionsheet = [[Uiactionsheet alloc] initwithtitle:@ " Is you sure? " Delegate:self cancelbuttontitle:@ "No way!" destructivebuttontitle:@ "Yes, I ' m sure!" Otherbuttontitles:nil]; [Actionsheet ShowInView:self.view];}
As shown in the preceding code, creating an action sheet requires multiple parameters:
(1) Initwithtitle: Sets the title, which will be displayed at the top of the action sheet
(2) Delegate: Sets the delegate for the action sheet. When a button of action sheet is pressed, its delegate will be notified, and the Actionsheet:diddismisswithbuttonindex method that will execute the delegate will execute. Here, we set the delegate to self, which guarantees the execution of our own Actionsheet:diddismisswithbuttonindex method written in viewcontroller.m.
(3) Cancelbuttontitle: Sets the title of the Cancel button, which will be displayed at the bottom of action sheet
(4) Destructivebuttontitle: Set the title of the first OK button, which can be understood as: "OK, continue"
(5) Otherbuttontitles: can set any number of OK button, want to add two buttons, can be written as:
Otherbuttontitles: @ "New button 1" @ "New button 2", nil
Notice that the last parameter is nil.
[Actionsheet ShowInView:self.view] This statement is used to display the action Sheet, exactly, this statement is to the action Sheet set the parent, and the parent must be a view, And is the view that is currently being displayed.
5, then, we add a method in VIEWCONTROLLER.M, the complete code is:
-(void) Actionsheet: (Uiactionsheet *) Actionsheet diddismisswithbuttonindex: (Nsinteger) Buttonindex { if ( Buttonindex! = [Actionsheet Cancelbuttonindex]) { nsstring *msg = nil; msg = @ "Can breathe easy, everything went ok."; Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Something is done" message:msg delegate:self cancelbuttontitle:@ "prew!" Otherbuttontitles:nil]; [Alert show];} }
This is how we touch the code that will execute after the action sheet. Since we previously set the action sheet delegate to self, this method will be called, and the parameter of this method buttonindex the number of the button that the user has touched, the button number is from top to bottom, starting from 0, for example, "Yes, I m sure!" This button is numbered 0 because it is the first OK button and the Cancel button is displayed at the bottom. The number of the Cancel button can be obtained directly from [Actionsheet Cancelbuttonindex].
Constructing an alert also needs to fill in many parameters:
(1) Initwithtitle: Set the title, will be displayed at the top of alert
(2) Message: Set the content of the prompt message
(3) Delegate: Sets the delegate for alert. Here, we set it to self.
(4) Cancelbuttontitle: Set the title of the Cancel button
(5) Otherbuttontitles: Similar to action sheet
[Alert show] This statement is used to display alert.
6, run a bit, see the effect it:
Note: The author says action sheet has at least two options, and can have one,
Uiactionsheet *actionsheet = [[Uiactionsheet alloc] initwithtitle:@ "Uiactionsheet <title>"
Delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "OK" otherbuttontitles:nil];
"Go" iOS development 6:uiactionsheet and Uialertview