欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS控件AlertView的使用

程序员文章站 2022-06-05 08:44:28
alert views are pop-up views that appear over the current view on the iphone. creating an...

alert views are pop-up views that appear over the current view on the iphone.

creating and showing an alert (arc compatible):

    uialertview *alert = [[uialertview alloc] initwithtitle:@"really reset?" message:@"do you really want to reset this game?" delegate:self cancelbuttontitle:@"cancel" otherbuttontitles:nil];
    // optional - add more buttons:
    [alert addbuttonwithtitle:@"yes"];
    [alert show];
for non-arc (retain/release) projects, you must autorelease the alert view:

    uialertview *alert = [[[uialertview alloc] initwithtitle:@"really reset?" message:@"do you really want to reset this game?" delegate:self cancelbuttontitle:@"cancel" otherbuttontitles:nil] autorelease];
    // optional - add more buttons:
    [alert addbuttonwithtitle:@"yes"];
    [alert show];
if you add the uialertviewdelegate protocol to your controller, you can also add the following method which is called after the user dismisses the alert view:

- (void)alertview:(uialertview *)alertview diddismisswithbuttonindex:(nsinteger)buttonindex {
    if (buttonindex == 1) {
        // do stuff
    }
}
button indices start at 0 (for the cancelbutton specified in the alloc/init), and go up by 1 for each addbuttonwithtitle call you add. if you have a lot of alerts, your diddismiss method can keep track of which one is being dismissed if you add the settag call to the alert initialization: [alert settag:23];

    uialertview *alert = [[[uialertview alloc] initwithtitle:@"error" message:@"i'm sorry dave, i'm afraid i can't do that." delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil] autorelease];
    [alert settag:12];
    [alert show];

... later ...

- (void)alertview:(uialertview *)alertview diddismisswithbuttonindex:(nsinteger)buttonindex {
    if ([alertview tag] == 12) {    // it's the error alert
        if (buttonindex == 0) {     // and they clicked ok.
            // do stuff
        }
    }
}

additional references