Detailed usage of UIAlertView and UIAlertController in IOS development and iosuialertview

Source: Internet
Author: User

Detailed usage of UIAlertView and UIAlertController in IOS development and iosuialertview

This article will explain the usage of UIAlertView and UIAlertController in IOS development from four aspects:

1. What is UIAlertView and UIAlertController? 2. Why should we use UIAlertView or UIAlertController? 3. How to Use UIAlertView and UIAlertController? 4. Read reminder.
1. What is UIAlertView and UIAlertController?

In a word, in all mobile apps, the prompt window is generally written by UIAlertView or UIAlertController. The two functions are the same.

 

2. Why should we use UIAlertView or UIAlertController?

Good human-computer interaction can improve user experience. The human-computer interaction function of the operating system is an important factor that determines the computer system's "friendliness. The human-computer interaction function relies mainly on external devices that can be input and output and corresponding software. In the traditional period, the devices that can be used for human-computer interaction include keyboard display, mouse, and various pattern recognition devices. The software corresponding to these devices is part of the human-computer interaction function provided by the operating system. Early Human-Computer Interaction facilities mainly involved keyboards and displays. The operator enters the command through the keyboard. After receiving the command, the operating system immediately executes the command and displays the result through the display. Commands can be entered in different ways, but the explanation of each command is clear and unique. Today, all the operations we perform on the mobile end are performed on the touch screen. For better human-computer interaction, the engineer displays the operation information or prompt information on the screen, the user determines the button to be clicked based on their own judgment. In a word, it is to use human-computer interaction to better improve the user experience and product design quality.

 

3. How to Use UIAlertView and UIAlertController? 1. How to Use UIAlertView: // Newly initialized alert view. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "Edit" message: @ "Please Modify the Info" delegate: self cancelButtonTitle: @ "Cancel" Tip: @ "Sure ", @ "Other", nil]; // use This property is inherited from the UIView, You can use this property to distinguish when a AlertView has multiple view alertView. tag = indexPath. row; // Adds a button to the receiver with the given title. [alertView addButtonWithTitle: @ "addBtn"];/** UIAlertViewStyle = four types of UIAlertViewStyleDefault, strong, // style of the password input box: UIAlertViewStylePlainTextInput, // normal input box style: UIAlertViewStyleLoginAndPasswordInput // account password box style * // An alert that allows the user to enter text. available in iOS 5.0 and later. alertView. alertViewStyle = UIAlertViewStylePlainTextInput; // Returns the text field at the given index UITextField * textField = [alertView textFieldAtIndex: 0]; textField. text = model. title; // The number of buttons on the alert view. (read-only) NSLog (@ "The total number of buttons is: % ld", alertView. numberOfButtons); // Returns the title of the button at the given index. NSLog (@ "The button title at the specified index: % @", [alertView buttonTitleAtIndex: 1]); // The index number of the cancel button. NSLog (@ "Index for the cancel button is: % ld", alertView. cancelButtonIndex); //-1 if no otherButtonTitles or initWithTitle :... not used NSLog (@ "The index of the first other button is (read-only): % ld", alertView. firstOtherButtonIndex); // show UIAlertView [alertView show];Some basic attributes of UIAlertView

 

 

 

/** // Process the click event Called when a button is clicked Based on the index of the clicked button. the view will be automatically dismissed after this call returns */-(void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex; // AlertView will be displayed soon-(void) willPresentAlertView :( UIAlertView *) alertView; // event when AlertView is displayed-(void) didPresentAlertView :( UIAlertView *) alertView; // events when ALertView is about to disappear // This method is invoked before the animation begins and the view is hidden. -(void) alertView :( UIAlertView *) alertView willDismissWithButtonIndex :( NSInteger) buttonIndex; // events executed when AlertView disappears // This method is invoked after the animation ends and the view is hidden. -(void) alertView :( UIAlertView *) alertView didDismissWithButtonIndex :( NSInteger) buttonIndex; // events of the cancel button of AlertView // If not defined in the delegate, we simulate a click in the cancel button-(void) alertViewCancel :( UIAlertView *) alertView;

 

2. How to Use UIAlertController:

* Using UIAlertController requires three steps.

* 1. instantiate alert: alertControllerWithTitle

* 2. instantiate the button: actionWithTitle

* 3. Display alertController: presentViewController

// 1. instantiate alert: Using * alertControl = [UIAlertController alertControllerWithTitle: @ "edit" message: @ "Change menu name:" preferredStyle: UIAlertControllerStyleAlert]; // 2. instantiate button: actionWithTitle // to prevent loop reference between the block and the Controller, we need _ weak to prevent _ weak typeof (alert) wAlert = alert; [alert addAction: [UIAlertAction actionWithTitle: @ "OK" style: UIAlertActionStyleDestructive handler: ^ (UIAlertAction * action) {// This block NSLog (@ "% @", [wAlert. textFields. firstObject text]);}]; [alert addAction: [UIAlertAction actionWithTitle: @ "cancel" style: UIAlertActionStyleCancel handler: nil]; // Add a text box (only the style that can be added to UIAlertControllerStyleAlert. If it is preferredStyle: custom, it will crash) [alert addTextFieldWithConfigurationHandler: ^ (UITextField * textField) {textField. text = model. title; // Method for listening for text changes [textField addTarget: self action: @ selector (textFieldsValueDidChange :) forControlEvents: UIControlEventEditingChanged] ;}]; [alert Syntax: ^ (UITextField * textField) {textField. secureTextEntry = YES; // display textField in ciphertext format. text = model. price;}]; // 3. show alertController: presentViewController [self presentViewController: alert animated: YES completion: nil];Basic use of UIAlertController

 

 

4. Reading reminder

In Xcode iOS8 SDK, both UIAlertView and UIAlertController are replaced by UIAlertController. Explanation of the official database:"UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead. "," UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of specified instead.". It indicates that in iOS8 + development, UIALertView and UIActionSheet are outdated. UIAlertController replaces the functions and functions of these two controls in a modular way.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.