UIAlertView 和UIActionSheet都继承于UIVIew
UIAlertView
初始化
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@”提示” message:@”释放删除” delegate:self cancelButtonTitle:@”cancel” otherButtonTitles:@”ok”,@”sayHi”, nil];
1.更改标题
alertView.title = @”提示1”;
2.增加一个选择按钮
[alertView addButtonWithTitle:@”123”];
3.显示在窗口
[alertView show];
释放
[alertView release];
4.取出警报视图里的两个UITextField输入框
UITextField *text1 = [alert textFieldAtIndex:0];
UITextField *text2 = [alert textFieldAtIndex:1];
给两个输入框更改弹出键盘
text1.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
text2.keyboardType = UIKeyboardTypeNumberPad;
5.设置提示视图风格
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
UIAlertView 一般不直接使用,一般在点击按钮的时候触发的 。
1.创建一个按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20, 20, 50, 20);
button.backgroundColor = [UIColor grayColor];
这里self指appDelegate
2.给这个按钮设置点击事件
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
3.实现点击事件触发方法
– (IBAction)buttonPressed:(UIButton *)button{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"dahk" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
给弹框里的按钮设置点击事件
初始化
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@”提示” message:@”请输入账户” delegate:self cancelButtonTitle:@”确认” otherButtonTitles:@”取消”, nil];
[alert show];
[alert release];
1.先遵守 < UIAlertViewDelegate >协议
@interface AppDelegate : UIResponder < UIApplicationDelegate,UIAlertViewDelegate >
2.实现协议里的方法
注意: buttonIndex根据每个按钮的位置0-1-2-3依次增加
– (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
//点击确认按钮 buttonIndex的值是0
NSLog(@"确认");
break;
case 1:
//点击取消按钮 buttonIndex的值是1
NSLog(@"取消");
break;
default:
break;
}
}
UIActionSheet
创建一个按钮
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.backgroundColor = [UIColor yellowColor];
button.frame = CGRectMake(100, 100, 100, 100);
[self.window addSubview:button];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
实现按钮点击事件触发方法
– (void)buttonClick:(UIButton *)button
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@”删除” delegate:self cancelButtonTitle:@”取消” destructiveButtonTitle:@”确认” otherButtonTitles: nil];
添加视图
[sheet showInView:self.window];
[sheet release];
}
1.先遵守 < UIActionSheetDelegate >协议
@interface AppDelegate : UIResponder < UIApplicationDelegate,UIActionSheetDelegate >
2.实现协议里的方法
注意: buttonIndex根据每个按钮的位置0-1-2-3依次增加
– (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
//点击确认按钮 buttonIndex的值是0
NSLog(@"确认");
break;
case 1:
//点击取消按钮 buttonIndex的值是1
NSLog(@"取消");
break;
default:
break;
}
}