4.UIAlertView和UIActionSheet(警报视图)知识总结

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;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值