iOS项目开发实战(Swift)—Button和Alert学习
来源:互联网 发布:arp检测软件 编辑:程序博客网 时间:2024/05/01 03:10
1.相关代码如下
iOS9.0中,苹果官方推荐使用UIAlertViewController取代之前UIAlertView,使用UIAlertView在Xcode7中会出现警告。
ViewController.swift代码如下:
import UIKitclass 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按钮之后,如下图:(刚好看到微信修改头像是这种操作,所以做成这样)
0 0
- iOS项目开发实战(Swift)—Button和Alert学习
- iOS项目开发实战(Swift)—项目目录和结构
- iOS项目开发实战(Swift)—初探UITabbarController和UINavigationController的集成
- iOS项目开发实战(Swift)—简单浏览器
- iOS项目开发实战(Swift)—查询天气小应用
- iOS项目开发实战(Swift)—View之间传递数据
- iOS项目开发实战(Swift)—初探TableView
- iOS项目开发实战(Swift)—代理的使用
- iOS项目开发实战(Swift)—View之间传递数据
- Swift语言IOS8开发战记1.Button与Alert
- iOS项目开发实战——开发基于Handoff的App(Swift)
- Swift开发iOS项目实战视频教程---掷骰子合集
- iOS开发-通过 objc_setAssociatedObject alert 和 button关联 及传值
- iOS开发项目实战——Swift实现图片轮播与浏览
- iOS开发项目实战——Swift实现ScrollView滚动条功能
- iOS项目开发实战——Swift实现多个TableView的侧滑与切换
- iOS开发(Swift)——Button的使用
- iOS开发——完整项目实战Swift篇&百思不得姐Swift版总结(一)
- 用canvas绘制圆点虚线的函数
- easyui常用控件常用方法
- 私服搭建
- android中的4种launchmode详解
- Android开发COM口调试技巧详解
- iOS项目开发实战(Swift)—Button和Alert学习
- Lucene 入门详解
- 08 网上商城项目实战之使用jersey实现应用服务器和图片服务器分离
- 可拖拽的闪亮view
- HDU 2874 Connections between cities(LCA离线算法)
- Spring中定时任务Quartz集群配置学习
- Fragment学习进阶(二)----->动态显示和移除
- 页面缓存OSCache
- scrollview里嵌套listview的问题(一)