
一、UIAlertView、 UIActionSheet都是ios系统自带的弹出式对话框,当UIAlertView或UIActionSheet弹出来时用户无法与应用界面中的其它控件交互,UIAlertView与AUIctionSheet最大的区别在于UIAlertView是表现为显示在屏幕*的弹出式警告框,而 ActionSheet表现为显示在屏幕底部的按钮列表。
1、创建UIAlertView。创建该对象时可致定改警告框的标题、消息内容、以及该警告框包含几个按钮等信息。如果程序需要监听用户点击饿警告框时那一个按钮,则需要在创建该UIAlertView时设置UIAlertViewDelegate委托对象。
2、调用UIAlertView显示。
3、如果需要监听用户点击了警告狂的那个按钮,则需为UIAlertViewDelegate协议中的方法。
二、UIActionSheet
UIActionSheet表现为显示在底部的按钮列表,用户通过单击某个按钮来表明自己的态度。默认情况下UIActionSheet只支持一个标题和多个按钮,UIActionSheet会有两个固定的按钮和多个其它按钮。
1、其创建与UIAlertView基本一致
三、具体代码如下:(包含相关的属性)
[alert show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"%ld",(long)buttonIndex);
btn.backgroundColor = [UIColor grayColor];
btn.frame = CGRectMake(10, 20, 80, 80);
[btn addTarget:self action:@selector(showAlert) forControlEvents:UIControlEventTouchUpInside];
if (textField.tag == 1) {
// [self showAlert];
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"男",@"女", nil];
action.tag = 3;
[action showInView:self.view];
[textField resignFirstResponder];
}
}
-(void)showAlert{
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"建行",@"农行", nil];
action.tag = 4;
[action showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"=====%zi",buttonIndex);
NSLog(@"---->%@",[actionSheet buttonTitleAtIndex:buttonIndex]);
if (actionSheet.tag == 3) {
UITextField *field = (UITextField *)[self.view viewWithTag:1];
field.text = [actionSheet buttonTitleAtIndex:buttonIndex];
}