IOS implementation Modal Dialog Box

Source: Internet
Author: User

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? "];

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.