Action sheet and alert are two special controls (for the moment, they are called controls. They are not actually controls, but two classes in IOS, these two Classes define two different types of pop-up boxes for user interaction.) The action sheet is displayed from the bottom, and there are two or more options for the user to choose from, alert is a warning box with one or more buttons on it for you to choose.
Before continuing this article, let's take a moment to talk about the delegate pattern (Delegate/Proxy mode) used in IOS ).
There are many defined classes in IOS that can be used directly when programming, such as uiactionsheet and uialertview. These Classes define many methods, we can call these methods without having to know how these methods are implemented. But there is a problem. If we want to change the implementation of these methods, what should we do? One method is inheritance. We can inherit a class and re-write the method in our class. This is a method, but it is not a very convenient method, sometimes you only need to change a very small function, but you need to inherit a very large class, it seems a bit complicated, and if you need some different implementations, then you need to define many different classes, which will be very troublesome. In order to make the development process more convenient, IOS uses another method to achieve the same goal, that is, using delegate, we use a defined class, then, use the delegate \ proxy to rewrite the method in the class. When the program is running, delegate finds that you have created an instance of a class and changed the method, in this way, the program will not call the original implementation (of course, you can also call the original implementation), but directly call the new implementation you wrote, this allows you to customize program methods.
The above statement may not be clear enough, and I also think it is vague. Let's look at the example below to explain how to use the delegate in IOS.
We continue with the previous project to implement the buttenpressed action of the button.
1) Add <uiactionsheetdelegate>
We need to use uiactionsheet in the bidviewcontroller class, And when using uiactionsheet, we need to implement one of its delegate (not all the delegate methods need to be implemented, as long as some methods are implemented according to actual needs, in this example, uialertview does not need to implement any delegate. However, this delegate is not implemented in the bidviewcontroller class. Therefore, you need to manually add it, but in the bidviewcontroller. add <uiactionsheetdelegate> in H to enable the bidviewcontroller to receive and respond to the proxy events of the uiactionsheet.
Open bidviewcontroller. h and add <uiactionsheetdelegate>
#import <UIKit/UIKit.h>@interface BIDViewController : UIViewController <UIActionSheetDelegate>@property (weak, nonatomic) IBOutlet UITextField *nameField;@property (weak, nonatomic) IBOutlet UITextField *numberField;......
2) Implement buttonpressed
Open bidviewcontroller. M, find the buttonpressed method, and add the following code:
- (IBAction)buttonPressed:(id)sender { UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"Are you sure?" delegate:self cancelButtonTitle:@"No Way!" destructiveButtonTitle:@"Yes, I'm Sure!" otherButtonTitles:nil]; [actionSheet showInView:self.view];}
Compile and run. After you click "do something" button, an action sheet will pop up from the bottom, as shown below:
OK. Based on this action sheet, we can analyze the meaning of each parameter in the uiactionsheet in the above Code:
[Uiactionsheet alloc]: Allocate memory space
Initwithtitle: @ "are you sure": Actionsheet title
Delegate: Self: Specifies the proxy of the uiactionsheet. Self indicates that the proxy is in this class, that is to say, find the implementation of the proxy method of uiactionsheet in the class where uiactionsheet is located (the class in this example is class bidviewcontroller ). Let's look back at the <uiactionsheetdelegate> We just added in bidviewcontroller. H, so that this class can receive and respond to the proxy event of uiactionsheet.
Cancelbuttontitle: @ "No way! ": Cancel button, used to cancel (do not continue the next operation). Here, set the text of the cancel button.
Destructivebuttontitle: @ "yes, I'm sure! ": It is equivalent to the "OK" button (continue to the next step). Here, set the text of the "OK" button.
Otherbuttontitles: Nil: In addition to the above cancel button and OK button, actionsheet can also Customize multiple buttons. Here, set the text of other buttons (for example, otherbuttontitles: @ "foo", @ "bar ", nil; the last parameter must be nil, indicating the end ).
The last line in the above Code:
[Actionsheet showinview: Self. View]
The role is to display the actionsheet. Each actionsheet requires a parent view and display itself in the parent view. Because we are a single view project and only one view, therefore, self. view is displayed in the view implemented by actionsheet.
3) Implement the actionsheet delegate Method
Add the following code under the buttonpressed method in bidviewcontroller. M:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ if(buttonIndex != [actionSheet cancelButtonIndex]) { NSString *msg = nil; if(nameField.text.length > 0) msg = [[NSString alloc] initWithFormat:@"You can breathe easy, %@, everything went OK.", nameField.text]; else msg = @"You can breathe easy, everything went OK."; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Something was done" message:msg delegate:self cancelButtonTitle:@"Phew!" otherButtonTitles:nil]; [alert show]; }}
The code above implements the delegate: diddismisswithbuttonindex of the action sheet. When you click the actionsheet button, the delegate is called instead of the original method. In this method, the user first determines that the cancelbutton is not clicked (based on the button Index). If the cancelbutton is not clicked (the destructivebutton is clicked because there are only two buttons ), A warning box is displayed.
Parameter description of uialertview:
[Uialertview alloc]: Allocate memory space
Initwithtitle: @ "something was done": Alert title
Message: msg: Alert text
Delegate: Self: The role is similar to that in actionsheet, except that uialertview does not implement any delegate method. Therefore, we have not introduced <uialertviewdelegate> in the header file.
Cancelbuttontitle: @ "Phew! ": The cancel button can be seen as a button to close the alert window, and no operations will continue.
Otherbuttontitles: Nil: Same as actionsheet
In general, the implementation of uialertview is quite similar to that of uiactionsheet.
4) Compile and run
Click do something to display the actionsheet
Click "no way! And the actionsheet disappears. Click Yes, I'm sure! The actionsheet disappears, and a warning box is displayed.
If you enter some content in namefield, alert displays
Click phew! Button. The warning box disappears.
The content of this article may be very easy for experts, but it may be confusing for new beginners. At least I have learned it for a long time, I also found a lot of information on the Internet to get a little understanding of it. I hope you can give us valuable comments. Thank you!
Control fun all