IOS implementation Modal Dialog Box
Reprinted for emergency purposes
Original article address:
Http://blog.csdn.net/xianpengliu/article/details/6591624
Next, encapsulate a class to implement this brief operation (the code is taken from the iPhone development cheat book):
Modalalert. h
# Import <uikit/uikit. h>
@ Interface modalalert: nsobject
+ (Bool) ask: (nsstring *) question;
+ (Bool) Confirm :( nsstring *) statement;
@ End
@ Interface modalalertdelegate: nsobject <uialertviewdelegate>
{
Cfrunloopref currentloop;
Nsuinteger index;
}
@ Property (readonly) nsuinteger index;
@ End
Modalalert. m
# Import "modalalert. H"
@ Implementation modalertdelegate
@ Synthesize index;
// Initialize with the supplied run Loop
-(ID) initwithrunloop: (cfrunloopref) runloop
{
If (Self = [Super init]) currentloop = runloop;
Return self;
}
// User pressed Button. Retrieve results
-(Void) alertview: (uialertview *) aview clickedbuttonatindex: (nsinteger) anindex
{
Index = anindex;
Cfrunloopstop (currentloop );
}
@ End
@ Implementation modalalert
+ (Nsuinteger) querywith: (nsstring *) question button1: (nsstring *) button1 button2: (nsstring *) button2
{
Cfrunloopref currentloop = cfrunloopgetcurrent ();
// Create alert
Modalalertdelegate * madelegate = [[modalalertdelegate alloc] initwithrunloop: currentloop];
Uialertview * alertview = [[uialertview alloc] initwithtitle: Question message: Nil delegate: madelegate cancelbuttontitle: button1 otherbuttontitles: button2, nil];
[Alertview show];
// Wait for response
Cfrunlooprun ();
// Retrieve answer
Nsuinteger answer = madelegate. index;
[Alertview release];
[Madelegate release];
Return answer;
}
+ (Bool) ask: (nsstring *) question
{
Return
[Modalalert querywith: Question button1: @ "no" button2: @ "yes"];
}
+ (Bool) confirm: (nsstring *) Statement
{
Return
[Modalalert querywith: Statement button1: @ "cancel" button2: @ "OK"];
}
@ End
The usage is as follows:
Nsuinteger answer = [modalalert ask: @ "Are you sure? "];