Prompt box controller :UIAlertController

Prompt box button :UIAlertAction
 
function : Used to prompt information , And give some buttons that can be selected to deal with the corresponding requirements .
 
Be careful : stay Xcode Of iOS8 SDK in ,UIAlertView and UIActionSheet All be UIAlertController replace . The official library explains : “UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.”、“UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead.” . It shows that iOS8+ Development ,UIALertView and UIActionSheet It's out of date ,UIAlertController Replace the functions and functions of these two controls in a modular way . How to create and use UIAlertController Become the focus of our attention .
 
Class introduction :
1、 Prompt box style enumeration ( It is divided into UIAlertView、UIActionSheet)

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {

UIAlertControllerStyleActionSheet = 0,  // A prompt box pops up at the bottom of the view , It can't add text boxes , And in ipad Must use popover Form show

UIAlertControllerStyleAlert                  // A prompt box pops up in the middle of the view

} NS_ENUM_AVAILABLE_IOS(8_0);

2、 The style of the buttons on the prompt box

typedef NS_ENUM(NSInteger, UIAlertActionStyle) {

UIAlertActionStyleDefault = 0,    // The default confirmation button

UIAlertActionStyleCancel,          // The default cancel button

UIAlertActionStyleDestructive    // The default red button

}NS_ENUM_AVAILABLE_IOS(8_0);

3、UIAlertController: Prompt box controller class

@interface UIAlertController : UIViewController

Method :

// Create the class method of the prompt box controller

+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;

// Add instance method of text box on prompt box ( Only in UIAlertView Style prompt box to add )

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

// Add a button to the prompt box

- (void)addAction:(UIAlertAction *)action;

attribute :

// The prompt box contains an array of all buttons

@property (nonatomic, readonly) NSArray *actions;

// The prompt box contains an array of all text boxes

@property (nonatomic, readonly) NSArray *textFields;

// The title of the prompt box

@property (nonatomic, copy) NSString *title;

// Prompt information

@property (nonatomic, copy) NSString *message;

// The style of the prompt box controller

@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;

@end

4、UIAlertAction: Prompt box button

@interface UIAlertAction : NSObject <NSCopying>

Method :

// Create the class method of the prompt box button

+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler;

attribute :

// Button title

@property (nonatomic, readonly) NSString *title;

// The style of the button

@property (nonatomic, readonly) UIAlertActionStyle style;

// Whether the button works

@property (nonatomic, getter=isEnabled) BOOL enabled;

@end

Specific examples are as follows :

Creating steps :

1、 Layout storyboards , Drag a button in the view of the controller , And associated with IBAction event

2、 The main code in the event associated with the button is as follows :

// Create a prompt box controller

 // Create a prompt box controller 
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@" Prompt box " message:@" news " preferredStyle:UIAlertControllerStyleAlert];
alertController.view.backgroundColor = [UIColor purpleColor];

// Create a prompt box button

 // Create prompt button 
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@" Default Cancel" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@" Default Default" style:UIAlertActionStyleDefault handler:nil]; UIAlertAction *action3 = [UIAlertAction actionWithTitle:@" Default Destructive" style:UIAlertActionStyleDestructive handler:nil];

// Add a prompt button to the prompt box

 // Add prompt button 
[alertController addAction:action1];
[alertController addAction:action2];
[alertController addAction:action3];

// Add a text box to the prompt box ( Only suitable for prompt box style is :UIAlertControllerStyleAlert)

 // Add text box ( Only suitable alertview Type prompt box )
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @" account number ";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @" password ";
textField.secureTextEntry = YES; // Safe input mode
}];

// Add a listening event to the text box

 // Add a listening event to the text box ( The beginning of the text box 、 end 、 State change, etc )
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @" Add listening Events "; [textField addTarget:self action:@selector(alertTextFiledDidChanged:) forControlEvents:UIControlEventEditingChanged];
}];

// Display the prompt box in the form of modal window

[self presentViewController:alertController animated:YES completion:nil];

// Implement textbox Events

#pragma mark Text box listening Events 
-(void)alertTextFiledDidChanged:(NSNotification *)notification
{
NSLog(@"Enditing changed");
}

// Click button , Show the demo results

When not added action3 Button to the prompt box , That is the number of buttons <=2 when , The style screenshots of the two prompt boxes are :

UIAlertControllerStyleAlert: Pop up in the middle of the screen

UIAlertControllerStyleActionSheet: Pop up from the bottom of the screen ( Cannot add text box )

When not added action3 Button to the prompt box , That is the number of buttons >=3 when , The style screenshots of the two prompt boxes are :

UIAlertControllerStyleAlert: Pop up in the middle of the screen

UIAlertControllerStyleActionSheet: Pop up from the bottom of the screen ( Cannot add text box )

iOS:UIAlertController and UIAlertAction More related articles with detailed explanation of

  1. iOS View controller transition details

    iOS View controller transition details Foreword of foreword Tang Qiao's predecessors are in the official account of WeChat 「iOSDevTips」 And after pushing my article on his blog , my Github There has been a substantial increase in various indicators , Thank you for your recommendation . Some people ask me related questions ...

  2. iOS Development of the photo framework (2)

    One . survey This article continues  iOS Development of the photo framework , This paper focuses on the introduction of PhotoKit And with ALAssetLibrary The difference of , And how to base it on PhotoKit And AlAssetLib ...

  3. iOS in MVC And other design patterns

    iOS in MVC And other design patterns stay iOS Programming , Using design patterns can greatly improve your development efficiency , Although you need to spend a lot of time encapsulating all kinds of business logic at the beginning of coding .( It turns out to be worth it !) Model - View - controller (MVC) Design ...

  4. iOS Certificates and signatures A detailed explanation of the puzzle

    iOS Certificates and signatures A detailed explanation of the puzzle classification : iPhone2012-06-06 19:57 9426 Human reading   Comment on (1)  Collection   report iosxcodecryptographyappleiphone test   Catalog ...

  5. iOS 6 Share list ——UIActivityViewController Detailed explanation

    iOS 6 Share list ——UIActivityViewController Detailed explanation 2013-06-03 01:42:33      Comment stay iOS 6 Then a shared list view is provided , It passes through UIActivity ...

  6. IOS Database operation SQLite3 The use of, ( turn )

    iPhone Medium support pass sqlite3 To visit iPhone Local database . The specific use method is as follows 1: Add development package libsqlite3.0.dylib The first is to set up the project file , Add... To the project iPhone Version of sqlite3 Number of numbers ...

  7. iOS The photo frame of development (2) —— PhotoKit Detailed explanation ( Next )

    Link to this article :http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html Here we go on <iOS ...

  8. iOS Development of the photo framework

    Reprinted from :http://kayosite.com/ios-development-and-detail-of-photo-framework.html One . Summary stay iOS In the device , Photos and videos are quite heavy ...

  9. iOS The photo frame of development (2) —— PhotoKit Detailed explanation ( On )

    Reprinted from :http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html One . survey This article continues  iOS open ...

Random recommendation

  1. jQuery Personalized image rotation effect

    jQuery Personalized image rotation effect Shopping product display : Picture carousel < The effect is as follows > Train of thought : Every once in a while , Realize the automatic switching of pictures and the effect of tab selection , Mouse in picture animation stop , Scratch or leave the animation to start Two regions : ...

  2. Want to do linux Friends of O & M Engineers , You have to master the following tools ...

    Want to do linux Friends of O & M Engineers , You have to master the following tools ...  [ Replication link ]   Published in  2013-12-13 15:59 |  come from   51CTO Webpage [ Just look at him ]  Landlord            I am ...

  3. Redis Use summary (2):Python Interface

    install redis-py sudo pip2 install redis a master hand 's first small display redis Connection instances are thread safe , You can directly redis Set the connection instance as a global variable and use it directly . If you need another one Redis example (o ...

  4. Google Translation request ( The difficulty is tk Parameters )

    Business needs to translate some words ··· But direct call interface charges ( It seems that the first few million words are free , And then they started charging )···· Just want to study Google Translation interface ... Want to simulate Google Send one to the server Http ...

  5. Application Ntop Monitoring network traffic ( video Demo)

    Application Ntop Monitoring network traffic ____ Network traffic reflects the running state of the network , It is the key data to judge whether the network is running normally , In the actual network , If the network traffic is not well controlled or network congestion occurs , It will lead to the decrease of network throughput . Network performance degradation . Through flow ...

  6. AspNet WebApi OData Study

    OData Introduce : It's a way to query and update data Web agreement .OData Applied web Technologies such as HTTP.Atom Release agreement (AtomPub) and JSON And so on , service And stored information access . In addition to providing some basic exercises ...

  7. About HTML And CSS Writing specifications

    I haven't noticed this before , Because when you see the source code of some excellent websites , Open what they quoted css The file format doesn't look standard either . But fortunately, what I happened to see yesterday was enhanced CSS Normalizing readability optimizes page performance , So I made up my mind to change . I ...

  8. Django Medium WSGI

    Django Source code profile : Django Medium WSGI 2013-09-06 22:31 by Troublemakers , 334  read , 0  Comment on ,  Collection ,  edit Django It's already built-in with a small device for local testing ...

  9. Java VS .NET:Java And .NET Compare the characteristics of

    One . Preface Why write Java Follow .NET contrast ? .NET I was born with Java Shadow . From imitation to innovation ,.NET The platform is becoming more and more mature . Their different supporters often argue about which is better . But this article is not for one point ...

  10. chrome Official complete installation package

    But did you know Google allows you to download the full standalone installer of Chrome from its offi ...