iOS中UIAlertView3秒后消失的兩種實(shí)現(xiàn)方法
一,效果圖。

二,代碼。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:nil message:@"此信息3秒后消失" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alert show];
[self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:3.0];
}
- (void)dismissAlert:(UIAlertView*)alert {
if ( alert.visible ) {
[alert dismissWithClickedButtonIndex:alert.cancelButtonIndex animated:YES];
}
}
下面給大家介紹下UIAlertView自動消失的兩種方法
話說,在寫程序的過程中用到很多提示的信息,于是非常自然地就要使用UIAlertView控件。
但是這些提示的信息有時候只需提示就行,不用操作,那么此時就要這個提示框自動消失就OK了。
UIAlertView彈出后2s讓其自動消失,兩種方法:
(1)結(jié)合NSTimer
UIAlertView baseAlert = nil;
- (void) performDismiss: (NSTimer *)timer
{
[baseAlert dismissWithClickedButtonIndex:0 animated:NO];//important
[baseAlert release];
baseAlert = NULL;
}
- (void) presentSheet
{
baseAlert = [[UIAlertView alloc]
initWithTitle:@"Alert" message:@"\nMessage Message Message "
delegate:self cancelButtonTitle:nil
otherButtonTitles: nil];
[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector: @selector(performDismiss:)
userInfo:nil repeats:NO];
[baseAlert show];
}
(2)使用PerformSelector:withObject:afterDelay:方法
- (void) dimissAlert:(UIAlertView *)alert
{
if(alert)
{
[alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];
[alert release];
}
}
- (void)showAlert{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil
cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[self performSelector:@selector(dimissAlert:) withObject:alert afterDelay:2.0];
}
總結(jié)
以上所述是小編給大家介紹的iOS中UIAlertView3秒后消失的兩種實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS利用AVPlayer播放網(wǎng)絡(luò)音樂的方法教程
最近工作中遇到了一個需求,需要做一個在線音樂類的APP,通過一段時間的努力實(shí)現(xiàn)了,所以這篇文章主要給大家介紹了關(guān)于iOS利用AVPlayer播放網(wǎng)絡(luò)音樂的方法教程,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05
Objective-C優(yōu)雅使用KVO觀察屬性值變化
這篇文章主要為大家介紹了Objective-C優(yōu)雅使用KVO觀察屬性值變化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
iOS10 widget實(shí)現(xiàn)3Dtouch 彈出菜單
這篇文章主要介紹了 iOS10 widget實(shí)現(xiàn)3Dtouch 彈出菜單的相關(guān)資料,需要的朋友可以參考下2016-12-12
iOS中UILabel text兩邊對齊的實(shí)現(xiàn)代碼
本文通過一段實(shí)例代碼給大家介紹了ios中uilabel text兩邊對齊的實(shí)現(xiàn)方法,非常不錯,具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
iOS中如何判斷當(dāng)前網(wǎng)絡(luò)環(huán)境是2G/3G/4G/5G/WiFi
這篇文章主要給大家介紹了關(guān)于iOS中如何判斷當(dāng)前網(wǎng)絡(luò)環(huán)境是2G/3G/4G/5G/WiFi的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
iOS 下拉刷新動畫的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了iOS 下拉刷新動畫的實(shí)現(xiàn)實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
iOS應(yīng)用開發(fā)中監(jiān)聽鍵盤事件的代碼實(shí)例小結(jié)
這篇文章主要介紹了iOS應(yīng)用開發(fā)中監(jiān)聽鍵盤事件的代碼實(shí)例小結(jié),呼出鍵盤等操作為iOS App中的必備功能,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03

