UIAlertView的使用介绍

本文详细介绍了UIAlertView的使用方法,包括如何创建UIAlertView实例、设置标题、消息文本、按钮等属性,以及如何通过代理方法来处理用户的交互行为。展示了UIAlertView的常用样式,并提供了如何根据用户输入启用或禁用按钮的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIAlertView的使用介绍
- (void)alert {

alert=[[UIAlertView alloc]initWithTitle:@”Message” message:@”Are you OK?” delegate:self cancelButtonTitle:@”cancel” otherButtonTitles:@”Yes”, nil];

alert.delegate=self;

//title可以更新alert的标题
alert.title=@”heheheda”;

//numerOfButtons只读属性,返回alert的按钮总数
NSLog(@”alert有%ld个button”,(long)alert.numberOfButtons);

//cancelbuttonIndex获得取消按钮的index,
NSLog(@”alert的取消button是%ld”,(long)alert.cancelButtonIndex);

NSLog(@”alert的第一个button是%ld”,(long)alert.firstOtherButtonIndex);

//alertView的style,必须设置alert的代理
//alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
alert.alertViewStyle=UIAlertViewStylePlainTextInput;

NSLog(@”%@”,[alert textFieldAtIndex:0]);

[alert show];

}

pragma mark UIAlertView Delegate

//delegater方法的调用顺序:willPresentAlertView–>alertViewShouldEnableFirstOtherButton–>didPresentAlertView–>clickedButtonAtIndex–>alertView:willDismissWithButtonIndex–>alertrView:didDismissWithButtonIndex

//获取用户点击的按钮,并做响应的处理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//buttonTitleAtindex:根据给的buttonIndex返回按钮的标题
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@”Yes”]) {
NSLog(@”点击了Yes按钮”);
//接受输入类容
//textFieldAtIndex 获取对应位置的UITextField
UITextField *textField = [alertView textFieldAtIndex:0];
NSLog(@”%@”,textField.text);
}

}

//当用户按下HOME键的时候,回调此方法,用户点击Cancel按钮的时候不会回调此方法
- (void)alertViewCancel:(UIAlertView *)alertView{
NSLog(@”点击了alertView的取消按钮”);

}

  • (void)willPresentAlertView:(UIAlertView *)alertView {

    NSLog(@”alertView即将显示”);
    }

  • (void)didPresentAlertView:(UIAlertView *)alertView{

    NSLog(@”alertView已经显示”);

}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:
(NSInteger)buttonIndex {

NSLog(@”alertView将消失”);

}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

NSLog(@”alertView已经消失”);

}

//一直在调用,设置alert中的A按钮禁用,如果满足条件,A按钮可用
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
UITextField *textField = [alertView textFieldAtIndex:0];
if (!textField.text || [textField.text isEqualToString:@”“])
{
NSLog(@”NO”);
return NO;
} else {
NSLog(@”yes”);
return YES;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员的修养

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值