) Basic usage of UIALertView and how to handle the event in the dialog box by using UIAlertViewDelegate.

Source: Internet
Author: User

(Conversion) basic usage of UIALertView and how to handle the event of the dialog box by UIAlertViewDelegate, uialertviewdelegate

First, the View Controller must implement the method in the Protocol UIAlertViewDelegate and specify the delegate as self so that the pop-up Alert window can respond to the click event. The Code is as follows:
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIAlertViewDelegate>@end

Detailed code in ViewController. m:

-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view from its nib // initialize AlertView UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "AlertViewTest" message: @ "message" delegate: self cancelButtonTitle: @ "Cancel" otherButtonTitles: @ "OtherBtn", nil]; // set the title and information, alert is usually used when the frame is used to initialize AlertView. title = @ "AlertViewTitle"; alert. message = @ "AlertViewMessage"; // This attribute is inherited from UIView. When there are multiple alertviews In a view, you can use this attribute to differentiate alert. tag = 0; // read-only attribute to check whether NSLog (@ "% d", alert. visible); // Add the button [alert addButtonWithTitle: @ "addButton"] through the given title; // NSLog Of the total number Of Buttons (@ "number Of Buttons: % d", alert. numberOfButtons); // obtain the NSLog (@ "buttonTitleAtIndex1: % @", [alert buttonTitleAtIndex: 1]); NSLog (@ "buttonTitleAtIndex2: % @", [alert buttonTitleAtIndex: 2]); // obtain the NSLog (@ "cancelButtonIndex: % d", alert. cancelButtonIndex); // obtain the index NSLog (@ "firstOtherButtonIndex: % d", alert. firstOtherButtonIndex); // display AlertView [alert show]; [alert release];} # pragma marks -- UIAlertViewDelegate -- // process click events based on the index of the clicked button-(void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {NSLog (@ "clickButtonAtIndex: % d", buttonIndex);} // event executed when AlertView disappears-(void) alertView :( UIAlertView *) alertView preview :( NSInteger) buttonIndex {NSLog (@ "Warn") ;}// events when ALertView is about to disappear-(void) alertView :( UIAlertView *) alertView cancel :( NSInteger) buttonIndex {NSLog (@ "willDismissWithButtonIndex");} // event of the cancel button of AlertView-(void) alertViewCancel :( UIAlertView *) alertView {NSLog (@ "alertViewCancel");} // event when AlertView is displayed-(void) Events :( UIAlertView *) alertView {NSLog (@ "didPresentAlertView ");} // when AlertView is to be displayed-(void) willPresentAlertView :( UIAlertView *) alertView {NSLog (@ "willPresentAlertView");}-(void) viewDidUnload {[super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self. myOutlet = nil ;}

 

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.