1. 程式人生 > >iOS開發- 自動消失的彈出框

iOS開發- 自動消失的彈出框

- (void)timerFireMethod:(NSTimer*)theTimer//彈出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert =NULL;
}


- (void)showAlert:(NSString *) _message{//時間
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"提示:" message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
   
    [NSTimer scheduledTimerWithTimeInterval:1.5f
                                     target:self
                                   selector:@selector(timerFireMethod:)
                                   userInfo:promptAlert
                                    repeats:YES];
    [promptAlert show];
}

在需要顯示彈出框的地方呼叫[self showAlert:@"測試"];即可。

原理: 彈出的時候, 設定一個定時器。 這裡設定1.5秒後, 把當前的彈出框置空。  (promptAlert =NULL;)