一、1.创建警告框的实例
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title"
message:@"message" delegate:nil cancelButtonTitle:@"NO"
otherButtonTitles:@"YES",nil];
2.设置警告框的代理对象,当用户对alertView有任何操作时,都会交给代理对象来进行响应
alert.delegate = self;
3.设置警告框的显示样式(共4种)
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField *textField = [alertView textFieldAtIndex:0];
设置警告框的样式
alertViewStyle属性:
1.UIAlertViewStyleDefault = 0默认
2.UIAlertViewStyleSecureTextInput 只有一个文本框且是密码框
3.UIAlertViewStylePlainTextInput 只有一个普通的文本框
4.UIAlertViewStyleLoginAndPasswordInput 有两个文本框,一个用户名,一个密码框
4.显示警告框
[alert show];
5.成为委托方代理需遵循协议<</span>UIAlertViewDelegate>,协议中方法有:
- (void)alertView:(UIAlertView *)alertView
clickeonAtIndex:(NSInteger)buttonIndex; //点击按钮携带文本框信息
- (void)alertViewCancel:(UIAlertView *)alertView;//点击cancel按钮
- (void)willPresentAlertView:(UIAlertView *)alertView;//警告框即将展现
- (void)didPresentAlertView:(UIAlertView *)alertView;//警告框已经展现
- (void)alertView:(UIAlertView *)alertView
willDismissWithButtonIndex:(NSInteger)buttonIndex;//文本框即将消失
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex;//文本框已经消失
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;//
二、1.创建操作表
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"title"
delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"危险"
otherButtonTitles:@"微博",@"微信",@"朋友圈", nil];
2.显示操作表
[sheet showInView:self.view];
3.操作表遵循的协议<</span>UIActionSheetDelegate>中的方法:
- (void)actionSheet:(UIActionSheet *)actionSheet
clickeonAtIndex:(NSInteger)buttonIndex;
- (void)actionSheetCancel:(UIActionSheet *)actionSheet;
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet;
- (void)actionSheet:(UIActionSheet *)actionSheet
willDismissWithButtonIndex:(NSInteger)buttonIndex;
- (void)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex;
4.在actionSheet中可以根据选中的按钮索引,获取按钮上的文字标题
NSString *selectedTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([selectedTitle isEqualToString:@"危险"]) {
NSLog(@"点击了危险按钮");
注意:此方法库iOS8以后的方法
1.UIAlertController 1.创建2.设置3.添加4.显示
step1:创建控制器实例
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:@"Title"
message:@"messsage"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction =
[UIAlertAction actionWithTitle:@"取消"
style: UIAlertActionStyleCancel
handler:^(UIAlertAction *action) { }];
[alertController addAction:cancelAction];
[alertController addTextFieldWithConfigurationHandler:
^(UITextField *textField) {
textField.secureTextEntry = YES;
}];
[self presentViewController:alertController
animated:YES completion:nil];