一,效果图。

二,代码。
?| 1 2 3 4 5 6 7 8 9 10 11 12 |
- (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)结合nstimer
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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:方法
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- (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];
}
|
总结
以上所述是小编给大家介绍的ios中uialertview3秒后消失的两种实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。