(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 ;}