iOS 中UIAlertView 的简单使用
1.创建
1.普通样式
NSString *title = @"我是标题"; // 标题
NSString *msg = @"你好"; // 你要显示的信息
NSString *cancel = @"取消"; // 取消键的标题 点击后 alertView会消失
NSString *other = @"其他:请点击"; // 其他按钮的标题,可以填写多个
// 这里主要不要把nil丢了,这是结束标志
// 设置了代理为self后要遵守UIAlertViewDelegate协议
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:cancel
otherButtonTitles:other, nil];
[alerView show]; // 这里是通过modal方式弹出
显示的样式就是这样了
如果需要添加多个选项按钮
可以通过创建
[alerView addButtonWithTitle:@"其他:1"];
添加多个选项按钮后 样式也会改变
也可以通过如下方法添加多个选项按钮
UIAlertView的选项按钮尽量不要设置太多
一到两个既是理想
若想要设置更多的按钮可以通过UIActionSheet来实现
NSString *title = @"我是标题"; // 标题
NSString *msg = @"你好"; // 你要显示的信息
NSString *cancel = @"取消"; // 取消键的标题 点击后 alertView会消失
NSString *other = @"其他:请点击"; // 其他按钮的标题,可以填写多个
NSString *other1 = @"其他1:请点击";
NSString *other2 = @"其他2:请点击";
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:cancel
otherButtonTitles:other,other1,other2, nil];
[alerView show];
如果添加按钮的标题过长
[alerView addButtonWithTitle:@"其他:过长过长过长过长过长过长过长过长过长过长过长过长过长过长过长过长"];
2.带输入框的样式
可以通过设置alertView的style设置样式
分别对应如下样式
alerView.alertViewStyle = UIAlertViewStylePlainTextInput;
UIAlertViewStylePlainTextInput这种是输入普通的文字
alerView.alertViewStyle = UIAlertViewStyleSecureTextInput;
UIAlertViewStyleSecureTextInput这种是输入密码的样式
alerView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UIAlertViewStyleLoginAndPasswordInput这种是输入登陆账号密码的样式
2.代理方法
可以通过下面方法取出对应按钮执行方法,响应用户的操作
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"点击了第%zi个按钮",buttonIndex);
}
如果控制器控制多个UIAlertView,那么回调方法可以通过设置alertView的tag来区别不同的UIAlertView.
用switch语句来对不同的UIAlertView进行响应