1. 程式人生 > >iOS-UIAlertView彈框2秒後自動消失

iOS-UIAlertView彈框2秒後自動消失

程式碼寫法如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    [selfshowAlert];    

}

/**

 *  彈框提示

 */

- (void)showAlert{

UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"2秒後將消失"delegate:

nilcancelButtonTitle:nilotherButtonTitles:nil];

    [alertshow];

// 2秒後執行

    [selfperformSelector:@selector(dimissAlert:)withObject:alertafterDelay:2.0];

}

/**

 *  移除彈框

 */

- (void) dimissAlert:(UIAlertView *)alert {

    if(alert){

        [alert dismissWithClickedButtonIndex:[alert cancelButtonIndex

animated:YES];

    }

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

}

@end

執行效果如下: