iOS项目开发实战(Swift)—Button和Alert学习

本文介绍了在iOS9项目开发中,如何使用Swift进行AlertViewController的替换,以解决UIAlertView在Xcode7中出现的警告问题。通过示例代码展示了创建Button触发Alert和ActionSheet的效果。

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

1.相关代码如下

iOS9.0中,苹果官方推荐使用UIAlertViewController取代之前UIAlertView,使用UIAlertView在Xcode7中会出现警告。

ViewController.swift代码如下:

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let button1 = UIButton()
        let button2 = UIButton()
        button1.frame = CGRectMake(50, 50, 100, 30)
        button1.setTitle("Alert", forState: UIControlState.Normal)
        button1.backgroundColor = UIColor.blackColor()
        button1.addTarget(self, action: "AlertClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button1)
        
        button2.frame = CGRectMake(50, 100, 100, 30)
        button2.setTitle("ActionSheet", forState: UIControlState.Normal)
        button2.backgroundColor = UIColor.blackColor()
        button2.addTarget(self, action: "ActionSheetClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button2)
        
        
    }
    //UIAlertController和UIAlertAction的使用
    func AlertClicked(sender: AnyObject){
        let alertcontroller = UIAlertController(title: "Clicked", message: "我是Alert", preferredStyle: UIAlertControllerStyle.Alert)
        
        let okAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: nil)
        
        let cancleAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
        
        //将action添加到视图控制器中
        alertcontroller.addAction(okAction)
        alertcontroller.addAction(cancleAction)
        //最后要呈现出视图控制器,否则报错error:Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
        self.presentViewController(alertcontroller, animated: true, completion: nil)
    }
    
    func ActionSheetClicked(sender: AnyObject){
        let alertcontroller = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        
        let okAction = UIAlertAction(title: "拍照", style: UIAlertActionStyle.Default, handler: nil)
        
        let selectAction = UIAlertAction(title: "从手机相册选择", style: UIAlertActionStyle.Default, handler: nil)
        
        let cancleAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
        
        alertcontroller.addAction(okAction)
        alertcontroller.addAction(selectAction)
        alertcontroller.addAction(cancleAction)
        //呈现出视图控制器
        self.presentViewController(alertcontroller, animated: true, completion: nil)

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}


2.command+R成功运行

点击Alert按钮之后,如下图:



点击ActionSheet按钮之后,如下图:(刚好看到微信修改头像是这种操作,所以做成这样)


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值