ios学习之创建上拉菜单的实现

本文详细介绍了如何使用UIAlertController创建上拉菜单,并通过设置样式为ActionSheet,实现类似于AlertView的效果。通过添加多个Action,可以实现丰富的交互功能。

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

上拉菜单实质上是和alertview一样的

只不过实在样式的改变方面用的是

UIAlertControllerStyle.ActionSheet这个

具体看代码

class ViewController: UIViewController {
//声明一个UIAlertController实例,以便在下方代码中进行调用
    var controller: UIAlertController?
    override func viewDidLoad() {
        //创建一个alertcontroller的实例 最上面的标题,还有信息,以上拉菜单actionsheet来显示出来
        controller = UIAlertController(title: "曹凯强", message: "爱我请点我", preferredStyle: UIAlertControllerStyle.ActionSheet)
        //创建在点击的时候该发生什么函数 action 应该创建多个action
               // Do any additional setup after loading the view, typically from a nib.
        //第一个action
        let action1 = UIAlertAction(title: "cao", style: UIAlertActionStyle.Default) { (paramAction:UIAlertAction) -> Void in
        }
        //第二个action
        let action2 = UIAlertAction(title: "cao", style: UIAlertActionStyle.Default) { (paramAction:UIAlertAction) -> Void in
        }
        //第三个action
        let action3 = UIAlertAction(title: "cao", style: UIAlertActionStyle.Destructive) { (paramAction:UIAlertAction) -> Void in
        }
        //添加多个action
        controller?.addAction(action1)
        controller?.addAction(action2)
        controller?.addAction(action3)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //应该是在主视图加载完成之后再去显示这个视图,所以要重写这个函数
    //viewDidAppear
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        //呈现这个controller的时候所用的方法
        self.presentViewController(controller!, animated: true, completion: nil)
    }
}

要创建多个action 以便于添加
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值