iOS9中UIAlertView和UIActionSheet的用法

本文介绍如何在 iOS 应用中使用 UIAlertController 创建弹窗,包括不同样式的配置及按钮事件处理。通过 UIButton 触发警报视图,并演示了确定、取消及警告按钮的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建一个button调用控件

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(100, 100, 100, 30);

    button.backgroundColor = [UIColorredColor];

    [button addTarget:selfaction:@selector(buttonclick:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

}

//button的方法实现

-(void)buttonclick:(UIButton *)sender{

    

    UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"yes"message:@"没有message"preferredStyle:UIAlertControllerStyleActionSheet];

//    preferredStyle  为样式,,有两个可以选择 UIAlertControllerStyleActionSheet 样式为actionsheet的样式  UIAlertControllerStyleAlertalertview的样式

    

    UIAlertAction *Sure = [UIAlertActionactionWithTitle:@"确定"style:(UIAlertActionStyleDefault)handler:^(UIAlertAction *action) {

        

        //添加确定的点击事件  点击事件写在这里

        

    }];//可以添加多个确定键

    UIAlertAction *cancel = [UIAlertActionactionWithTitle:@"取消"style:(UIAlertActionStyleCancel)handler:^(UIAlertAction *action) {

        

        

        //添加取消的点击事件

        

        

    }];//只能添加一个取消按钮

    

    UIAlertAction *alert = [UIAlertActionactionWithTitle:@"警告"style:(UIAlertActionStyleDestructive)handler:^(UIAlertAction *action) {

        //添加警告的点击事件

    }];

    

//    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

//        textField.placeholder = @"填写";

//        

//    }];  //只有在alertView的样式下才可以添加

    

    [alertController addAction:Sure];

    [alertController addAction:cancel];

    [alertController addAction:alert];

    

    [selfpresentViewController:alertControlleranimated:YEScompletion:nil];

}



demo下载地址   https://github.com/yongchangye/ios9AlertView





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值