#pragma mark - 1.基本用法
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Test" message:@"显示内容" delegate: nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
[alertView show];
#pragma mark - 2.多个按钮
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Test2" message:@"message" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"其他", nil];
[alertView show];
#pragma mark - 3.一些系统样式参数
/*
UIAlertViewStyle这个枚举提供了几个样式
1 typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
2 UIAlertViewStyleDefault = 0, 缺省样式
3 UIAlertViewStyleSecureTextInput, 密文输入框
4 UIAlertViewStylePlainTextInput, 明文输入框
5 UIAlertViewStyleLoginAndPasswordInput 登录用输入框,有明文用户名,和密文密码输入二个输入框
}
*/
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alertView show];
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
#pragma mark - 4. 判断用户点击了哪个按钮
首先遵守协议
@interface AppDelegate ()<UIAlertViewDelegate>
@end
方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ //判断点击了哪个按钮
NSLog(@"%ld",buttonIndex);
//如果是有输入框的
if (buttonIndex == 1) {
NSLog(@"Login = %@",[alertView textFieldAtIndex:0].text);
NSLog(@"PassWorld = %@",[alertView textFieldAtIndex:1].text);
}else{
NSLog(@"退出");
}
}
- (void)buttonpressed{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Test" message:@"显示内容" delegate: self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
[alertView show];
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
}
实现代码:(点击button触发 )
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 200, 30);
button.center = CGPointMake(_window.frame.size.width/2, _window.frame.size.height/2);
[button setTitle:@"button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonpressed) forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:button];
#pragma mark - 5.添加子视图(IOS7不能添加子视图了)
// UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"请等待"
// message:nil
// delegate:nil
// cancelButtonTitle:nil
// otherButtonTitles:nil,
// nil];
//
// UIActivityIndicatorView *activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// activeView.center = CGPointMake(view.bounds.size.width/2.0f, view.bounds.size.height-40.0f);
// [activeView startAnimating];
// [view addSubview:activeView];
//
// [view show];