UIAlertView的自动消失、手动消失、自定义消失

UIAlertView的消失本质其实是触发了一个dismiss事件。

触发这个事件有以下两种方法:

1.按钮点击,UIAlertView上如果有按钮,点击任何按钮都会触发该事件,UIAlertView消失;

2.代码模拟点击

 

[AlertObject dismissWithClickedButtonIndex:0 animated:NO]

1。创建一个自动消失的UIAlertView,这种消失方法的本质其实就是:使用定时器+代码模拟点击

 

-(void) performDismiss:(NSTimer *)timer
{
[Alert dismissWithClickedButtonIndex:0 animated:NO];
[Alert release];
}

-(void)presentAlert
{
Alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"出错啦!" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];

[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
[Alert show];
}

2.手动消失

如果你的UIAlertView上有按钮的话,点击任何一个按钮,都会触发一个dismiss事件,引起UIAlertView的消失。

3.自定制的消失,其实就是在自己认为合适的时候模拟点击。

下面这个例子是在textField的delegate执行即点击returnKey且输入昵称合法的时候,UIAlertView消失。

 

- (void) enterNickName:(NSString*)msg
{
prompt = [[UIAlertViewalloc] initWithTitle:@"设置昵称"
message:msg
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];

UITextField *textField = [[UITextFieldalloc] initWithFrame:CGRectMake(27.0,70.0, 230.0, 30.0)];
[textField setBackgroundColor:[UIColorwhiteColor]];
[textField setPlaceholder:@"输入昵称"];
textField.layer.cornerRadius=6.0;
textField.delegate=self;
textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
[prompt addSubview:textField];
[textField becomeFirstResponder];
textField.keyboardType=UIKeyboardTypeDefault;
textField.keyboardAppearance=UIKeyboardAppearanceDefault;
textField.returnKeyType=UIReturnKeyDone;
[textField release];
[promptsetTransform:CGAffineTransformMakeTranslation(0.0, -40.0)]; //可以调整弹出框在屏幕上的位置
[prompt show];
}

#pragma textField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"nickname=%@",textField.text);
if (nil==textField.text ||0==[[textField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length])
{
prompt.message=@"\n\n昵称不能为空!";
return YES;
}
if ([[textField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length]<2
||[[textField.textstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] length]>15)
{
prompt.message=@"\n\n昵称只支持2-15个字,请重新设置";
return YES;
}
[prompt dismissWithClickedButtonIndex:0animated:NO];//触发dismiss
[prompt release];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值