UIAlertAction的用法

let alertController =UIAlertController(title:"系统提示",

                                                message:"您确定要退出程序吗?", preferredStyle: .alert)

        let cancelAction =UIAlertAction(title:"取消", style: .cancel, handler:nil)

        let okAction =UIAlertAction(title:"好的", style: .default, handler: {

            action in

            print("点击了确定")

        })

        alertController.addAction(cancelAction)

        alertController.addAction(okAction)

        self.present(alertController, animated:true, completion: nil)

    }



 ///1.点击不响应提示框

    func mAlert(title:String,message:String) {

        let alert = UIAlertView(title: title, message: message, delegate: nil, cancelButtonTitle: "确定")

        alert.show()

    }


///2.带确认的提示框

    func OkAlert(title:String,message:String,vc: UIViewController ,sure: @escaping ()->Void) {

        let alertController = UIAlertController(title: title, message: message, preferredStyle:.alert)

        // 设置2个UIAlertAction

        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)

        let okAction = UIAlertAction(title: "确定", style: .default) { (UIAlertAction) in

            sure()

        }

        // 添加

        alertController.addAction(cancelAction)

        alertController.addAction(okAction)

        // 弹出

        vc.present(alertController, animated: true, completion: nil)

    }




///3.带输入框提示框

    func InputAlert(title:String,message:String,placeholder:String,vc: UIViewController ,sure: @escaping (_ text:String)->Void) {

        let alertController = UIAlertController(title: title, message:message, preferredStyle: UIAlertControllerStyle.alert)

        

        alertController.addTextField { (textField:UITextField) in

            textField.placeholder = placeholder

        }

        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)

        let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.default) { (UIAlertAction) in

            let login = alertController.textFields![0]

           sure(login.text!)

            print("输入的是:\(login.text)")

        }

        

        alertController.addAction(cancelAction)

        alertController.addAction(okAction)

        // 弹出

        vc.present(alertController, animated: true, completion: nil)

    }

    

}

/**

 使用示例:

 

1. WSAlert.API.mAlert(title: "提示", message: "看看")

 

 

2. //        WSAlert.API.InputAlert(title: "提示", message: "输入打赏金额", placeholder: "不大于1000", vc: self){(text) in

 //        print("输入:\(text)")

 //        }

 

 

 3.    WSAlert.API.OkAlert(title: "提示", message: "是否分享",vc:self){[weakSelf = self] in

 

 WSShareView.shareview.showInView(view: weakSelf.view, type: .GeneralShare, shareImageURL: "分享图片url" as AnyObject, shareContent: "分享内容", shareTitle: "分享标题", shareUrl: "分享链接")

 WSShareView.shareview.isShareSuccess = {(backMsg) in

 print("结果:\(backMsg)")

 }

 

 

 */




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
若要自定义 UIAlertAction,您可以创建一个自定义的 UIAlertAction 子类,并在其中重写相关方法。下面是一个示例代码: ```objective-c @interface CustomAlertAction : UIAlertAction @end @implementation CustomAlertAction - (instancetype)initWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler { self = [super initWithTitle:title style:style handler:handler]; if (self) { // 在这里自定义 UIAlertAction 的其他属性和样式 // 例如,修改字体、颜色等 NSDictionary *attributes = @{ NSFontAttributeName: [UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName: [UIColor redColor] }; NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attributes]; [self setValue:attributedTitle forKey:@"attributedTitle"]; } return self; } @end ``` 在上面的代码中,我们创建了一个名为 CustomAlertAction 的自定义 UIAlertAction 子类。在该子类中,我们重写了 initWithTitle:style:handler: 方法,并在其中添加了自定义的属性和样式。 在这个示例中,我们创建了一个字体为 20 的粗体字,并将标题的颜色设置为红色。然后,使用富文本属性将自定义的标题应用到 UIAlertAction 中。 要使用自定义的 UIAlertAction,可以按照以下步骤: ```objective-c UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleAlert]; CustomAlertAction *action = [[CustomAlertAction alloc] initWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 点击确定按钮后的处理逻辑 }]; [alertController addAction:action]; [self presentViewController:alertController animated:YES completion:nil]; ``` 在上面的示例中,我们创建了一个 CustomAlertAction 的实例,并使用它作为 UIAlertAction 添加到 UIAlertController 中。 这样,您就可以自定义 UIAlertAction 并应用自定义的样式了。如果有其他问题,请随时提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值