What is UIAlertView?
1. Basic usage
1 UIAlertView * view = [[UIAlertView alloc] initWithTitle: @ "Test" // Title
2 message: @ "this is a alert view" // display content
3 delegate: nil // delegate. You can click an event to process it.
4 cancelButtonTitle: @ "cancel"
5 otherButtonTitles: @ "OK"
6 //, @ "other", // Add other buttons
7 nil];
8 [view show];:
2. Multiple buttons
After you cancel the annotation of the above Code @ "others", the running effect is as follows:
You can add multiple
3. Some system style parameters
UIAlertViewStyle this enumeration provides several styles
1 typedef NS_ENUM (NSInteger, UIAlertViewStyle ){
2 UIAlertViewStyleDefault = 0, // default Style
3 UIAlertViewStyleSecureTextInput, // ciphertext input box
4 UIAlertViewStylePlainTextInput, // plaintext input box
5 UIAlertViewStyleLoginAndPasswordInput // The logon user input box contains the user name and the ciphertext password.
6 };
The Code is as follows:
1 UIAlertView * view = [[UIAlertView alloc] initWithTitle: @ "Please wait" // Title
2 message: @ "this is a alert view" // display content
3 delegate: nil // delegate. You can click an event to process it.
4 cancelButtonTitle: @ "cancel"
5 otherButtonTitles: @ "OK ",
6 //, @ "other", // Add other buttons
7 nil];
8 [view setAlertViewStyle: UIAlertViewStyleLoginAndPasswordInput]; // control the style:
This is the parameter: UIAlertViewStyleLoginAndPasswordInput. You can view other parameters by yourself.
However, I personally think it is too ugly to accept these types, So I customize a pop-up box to accept the input.
Implementation is not difficult. If you need it, contact me.
4. Determine which button the user has clicked
UIAlertView delegate UIAlertViewDelegate to implement click events as follows:
. H file
1 @ interface ViewController: UIViewController <UIAlertViewDelegate> {
2
3}
Delegate in. m
1-(void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex
2 {
3 NSString * msg = [[NSString alloc] initWithFormat: @ "The % d button you pressed! ", ButtonIndex];
4 NSLog (@ "% @", msg );
5} In this method, the parameter buttonIndex indicates the index of the button. The three buttons are "cancel", "OK", and the indexes corresponding to "other" are "0", respectively ", "1", "2 ".
When processing a click in Delegate mode, it will cause a problem. For example, when there are several uialertviews in a page, processing the click will increase the complexity of the processing logic, make some judgments
In this case, a solution is to use Block to add the callback of the Block, instead of Delegate, target and selector. (This content will be expanded next time)
5. Add a subview
This is also a lot of use cases.
Add UIActivityIndicatorView
Implementation Code:
1 UIAlertView * view = [[UIAlertView alloc] initWithTitle: @ "Please wait"
2 message: nil
3 delegate: nil
4 cancelButtonTitle: nil
5 otherButtonTitles: nil,
6 nil];
7
8 UIActivityIndicatorView * activeView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
9 activeView. center = CGPointMake (view. bounds. size. width/2.0f, view. bounds. size. height-40.0f );
10 [activeView startAnimating];
11 [view addSubview: activeView];
12
13 [view show];
Add UITableView
Several lines of code in this list are unclear. Let's talk about the idea. The reason why UIAlertView has such a large space to display UITableView is a clever method.
1 UIAlertView * view = [[UIAlertView alloc] initWithTitle: @ "select"
2 message: @ "\ n"
3 delegate: nil
4 cancelButtonTitle: nil
5 otherButtonTitles: nil,
6 nil];
7 // 10 linefeeds are used to support the large UIAlertView, and then UITableView is added. You can implement this by yourself. If necessary, leave a message.
Basically, this is a common and practical thing, and another important thing is to customize and beautify UIAlertView. I believe many people care about this, the custom and beautifying content will be detailed in the next article, analyzing the Demo source code that I personally think is good.