iPhone控件之UIAlertView

#import "UITestViewController.h"

NSTimer *timer;

@implementation UITestViewController

- (void)hideAlert:(NSTimer *)sender
{
UIAlertView *alert = [sender userInfo];

[alert dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)viewDidLoad {

[super viewDidLoad];

UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"title"
message:@"This alert will disappear in 3 seconds"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];

[myAlert addButtonWithTitle:@"new button"];
[myAlert addButtonWithTitle:@"another button"];

[myAlert show];

//create a timer to hide the alert automatically in 3 seconds
timer = [[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(hideAlert:) userInfo:myAlert repeats:NO] retain];

[myAlert release];
}
原文地址:https://www.cnblogs.com/foxmin/p/2393607.html