In the project, I encapsulate the pop-up and click events in a tool class. It is important to implement the Protocol in the tool class when calling this tool class, the method of this Protocol is to tell the page which button of the dialog box is clicked, and then the page can respond to different events based on different values passed in. The Code is as follows:
. H file
[Cpp]
# Import <Foundation/Foundation. h>
@ Protocol myAlertviewutilDelegate <NSObject>
-(Void) myalertviewbuttonclik :( int) num;
@ End
@ Interface AlertViewUtil: NSObject <UIAlertViewDelegate> {
// Custom dialog box Protocol
Id <myAlertviewutilDelegate> utildelegate;
UIAlertView * myonealertview; // a dialog box with a button
UIAlertView * mytwoalertview; // dialog box with two buttons
UIAlertView * mythreealertview; // dialog box with three buttons
}
@ Property (nonatomic, retain) id <myAlertviewutilDelegate> utildelegate;
-(Void) alertviewutilone :( NSString *) buttontitleone addmessage :( NSString *) message;
-(Void) alertviewutiltwo :( NSString *) buttontitleone twobutton :( NSString *) buttontitletwo addmessage :( NSString *) message;
-(Void) alertviewutilthree :( NSString *) buttontitleone twobutton :( NSString *) buttontitletwo threebutton :( NSString *) buttontitlethree addmessage :( NSString *) message;
@ End
. M file
[Cpp]
# Import "AlertViewUtil. h"
@ Implementation AlertViewUtil
@ Synthesize utildelegate;
-(Void) dealloc {
[Utildelegate release];
[Super dealloc];
}
// Implement the button clicking method in the system conversation Basket Protocol
-(Void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {
// Click the first alertview
If (alertView = myonealertview ){
If (buttonIndex = 0 ){
[Self. utildelegate myalertviewbuttonclik: 11];
}
[Myonealertview release];
}
// Click the second alertview.
Else if (alertView = mytwoalertview ){
If (buttonIndex = 0 ){
[Self. utildelegate myalertviewbuttonclik: 21];
}
Else if (buttonIndex = 1 ){
[Self. utildelegate myalertviewbuttonclik: 22];
}
[Mytwoalertview release];
}
// Click the third alertview.
Else if (alertView = mythreealertview ){
If (buttonIndex = 0 ){
[Self. utildelegate myalertviewbuttonclik: 31];
}
Else if (buttonIndex = 1 ){
[Self. utildelegate myalertviewbuttonclik: 32];
}
Else if (buttonIndex = 2 ){
[Self. utildelegate myalertviewbuttonclik: 33];
}
[Mythreealertview release];
}
}
// Alertview with only one button
-(Void) alertviewutilone :( NSString *) buttontitleone addmessage :( NSString *) message {
Myonealertview = [[UIAlertView alloc] initWithTitle: @ "prompt" message: message delegate: self cancelButtonTitle: nil otherButtonTitles: buttontitleone, nil];
[Myonealertview show];
}
// The alertview of the two buttons
-(Void) alertviewutiltwo :( NSString *) buttontitleone twobutton :( NSString *) buttontitletwo addmessage :( NSString *) message {
Mytwoalertview = [[UIAlertView alloc] initWithTitle: @ "prompt" message: message delegate: self cancelButtonTitle: nil otherButtonTitles: buttontitleone, buttontitletwo, nil];
[Mytwoalertview show];
}
// Alertview with three buttons
-(Void) alertviewutilthree :( NSString *) buttontitleone twobutton :( NSString *) buttontitletwo threebutton :( NSString *) buttontitlethree addmessage :( NSString *) message {
Response = [[UIAlertView alloc] initWithTitle: @ "prompt" message: message delegate: self cancelButtonTitle: nil otherButtonTitles: buttontitleone, buttontitletwo, buttontitlethree, nil]
[Mythreealertview show];
}
@ End
From RiverAM's column