ios 程序设计 (2016.3@UniTown) 第八讲 : 可视化控件的使用 (UIControls) 华南师范大学网络教育学院 2016-04-19
截 到 2016.2 ios 版本的占有率 Feb 2016 潘战生华南师范大学网络教育学院 http://www.gdou.com
潘战生华南师范大学网络教育学院 http://www.gdou.com
潘战生华南师范大学网络教育学院 http://www.gdou.com
潘战生华南师范大学网络教育学院 http://www.gdou.com
潘战生华南师范大学网络教育学院 http://www.gdou.com
第七章 UI 控件 1. UISlider var value: Float value 缺省情况下值在 0-1.0 之间 可在 Value Changed 事件中响应值的变化 : @IBAction func valuechanged(sender: AnyObject) {... } 潘战生华南师范大学网络教育学院 http://www.gdou.com
第七章 UI 控件 1. UISlider var value: Float value 缺省情况下值在 0-1.0 之间 可在 Value Changed 事件中响应值的变化 : @IBAction func valuechanged(sender: AnyObject) {... } 潘战生华南师范大学网络教育学院 http://www.gdou.com
第七章 UI 控件 1. UISlider var value: Float value 缺省情况下值在 0-1.0 之间 可在 Value Changed 事件中响应值的变化 : @IBAction func valuechanged(sender: AnyObject) {... } 潘战生华南师范大学网络教育学院 http://www.gdou.com
代码创建 UISlider: var frame: CGRect = CGRectMake(198.0, 12.0, 94.0, 27.0) var sliderctl = UISlider(frame:frame) sliderctl.addtarget(self, action:selector( slideraction:"), forcontrolevents:uicontrolevents.valuechanged) self.view.addsubview(sliderctl) @IBAction func valuechanged(sender: AnyObject) {... } 潘战生华南师范大学网络教育学院 http://www.gdou.com
2. UISwitch var on:bool on 的值为 true 或 false 可在 Value Changed 事件中响应 on 值的变化 : @IBAction func switchvaluechanged(sender: AnyObject) {... } 潘战生华南师范大学网络教育学院 http://www.gdou.com
3. UIPickerView 提供列表数据供用户选择 (ios 7 以前 ): 潘战生华南师范大学网络教育学院 http://www.gdou.com
3. UIPickerView 提供列表数据供用户选择 (ios 7 以后 ):
UIPickerView row title component component 潘战生华南师范大学网络教育学院 http://www.gdou.com
UIPickerView func reloadallcomponents() func reloadcomponent(_ component:) func selectedrowincomponent(_ component:)->int func selectrow(_ row:, incomponent:,animated:) 潘战生华南师范大学网络教育学院 http://www.gdou.com
UIPickerView delegate UIPickerView UIPickerViewDelegate datasource UIPickerView ( Component row ) UIPickerViewDataSource UIPickerView 潘战生华南师范大学网络教育学院 http://www.gdou.com
UIPickerViewDelegate component pickerview(_ pickerview:, didselectrow:, incomponent:) component row pickerview(_ pickerview:, titleforrow:, forcomponent:) 潘战生华南师范大学网络教育学院 http://www.gdou.com
UIPickerViewDataSource numberofcomponentsinpickerview(_ pickerview:)->int pickerview(_ pickerview:, numberofrowsincomponent:) 潘战生华南师范大学网络教育学院 http://www.gdou.com
UITextField 可编辑的单行文本 属性 text 为文本内容 键盘的消除 (dismiss) 可实现 UITextFieldDelegate 的 textfieldshouldreturn(_ textfield:)->int textfield.resignfirstresponder() 潘战生华南师范大学网络教育学院 http://www.gdou.com
4.UISegmentedControl UISegmentedControl UIControlEvents.ValueChanged @IBAction func buttonpressed(sender:anyobject) { var control=sender as UISegmentedControl if control.selectedsegmentindex == 0{... } } 潘战生华南师范大学网络教育学院 http://www.gdou.com
5. UIToolBar 先看看处于导航环境下 UIViewController 的导航栏及 工具栏 潘战生华南师范大学网络教育学院 http://www.gdou.com
http://www.gdou.com
http://www.gdou.com
导航栏显示当前视图控制器标题 (title) 及其导航栏工具 (navigationitem) http://www.gdou.com
导航栏显示当前视图控制器标题 (title) 及其导航栏工具 (navigationitem) 工具栏显示当前视图控制器工具栏 toolbaritems http://www.gdou.com
导航栏显示当前视图控制器标题 (title) 及其导航栏工具 (navigationitem) 工具栏显示当前视图控制器工具栏 toolbaritems http://www.gdou.com
导航栏显示当前视图控制器标题 (title) 及其导航栏工具 (navigationitem) 工具栏显示当前视图控制器工具栏 toolbaritems 视图区显示当前视图控制器视图 (view) http://www.gdou.com
导航栏显示当前视图控制器标题 (title) 及其导航栏工具 (navigationitem) 工具栏显示当前视图控制器工具栏 toolbaritems 视图区显示当前视图控制器视图 (view) http://www.gdou.com
导航栏显示当前视图控制器标题 (title) 及其导航栏工具 (navigationitem) 工具栏显示当前视图控制器工具栏 toolbaritems 视图区显示当前视图控制器视图 (view) 左上角 后退 按钮显示前一个视图控 制器标题 (title) 文本 http://www.gdou.com
UIViewController 视图控制器中 : navigationitem leftbarbuttonitem(s) rightbarbuttonitem(s) toolbaritems http://www.gdou.com
从导航控制器来看 : // 获取程序主窗口对象 var win:uiwindow = UIApplication.sharedApplication().delegate.window! // 获取导航控制器对象 var nav:uinavigationcontroller = UINavigationController(rootViewController: win.rootviewcontroller) // 导航控制器对象的 navigationbar 属性 var nb = nav.navigationbar // items 的数目为 UINavigationItem 堆栈内的项数 var items = nb.items // 每个 item 为堆栈中视图控制器的导航项 var ni = items[0] as UINavigationItem var nbbi = ni.rightbarbuttonitem UINavigationController 的 toolbar 属性类似 http://www.gdou.com
从导航控制器来看 : UINavigationController 的 toolbar 属性类似 http://www.gdou.com
Storyboard UIToolBar UIToolBar items var items[anyobject] UIbarButtonItem :BarButtonItemAndToolBar 潘战生华南师范大学网络教育学院 http://www.gdou.com
Return Data 示例 : Model View
: class MasterViewController : UITableViewController{ var allitems:array? var databack:string? // } class ModalViewViewController : UIViewController{ var mydata:string? var master:masterviewcontroller?... }
:
: // delegate protocol,used for modalviewcontroller post data back protocol ModalViewControllerDelegate{ func willdismissmodalview(_ sender:anyobject) }
: // delegate protocol,used for modalviewcontroller post data back protocol ModalViewControllerDelegate{ func willdismissmodalview(_ sender:anyobject) } class MasterViewController : UITableViewController, <ModalViewControllerDelegate>{ var allitems:array?... }
: // delegate protocol,used for modalviewcontroller post data back protocol ModalViewControllerDelegate{ func willdismissmodalview(_ sender:anyobject) } class MasterViewController : UITableViewController, <ModalViewControllerDelegate>{ var allitems:array?... } class ModalViewViewController : UIViewController{ var mydata:string weak var delegate:modalviewcontrollerdelegate? @IBAction func doneaction(_ sender) @IBAction func CancelAction(_ sender) }
: // delegate protocol,used for modalviewcontroller post data back protocol ModalViewControllerDelegate{ func willdismissmodalview(_ sender:anyobject) } class MasterViewController : UITableViewController, <ModalViewControllerDelegate>{ 避免 Retain Cycle: var allitems:array?... } class A{ var b:b } class B{ var a:a class ModalViewViewController : UIViewController{ } var mydata:string weak var delegate:modalviewcontrollerdelegate? @IBAction func doneaction(_ sender) @IBAction func CancelAction(_ sender) }
: // delegate protocol,used for modalviewcontroller post data back protocol ModalViewControllerDelegate{ func willdismissmodalview(_ sender:anyobject) } class MasterViewController : UITableViewController, <ModalViewControllerDelegate>{ var allitems:array?... } class ModalViewViewController : UIViewController{ var mydata:string weak var delegate:modalviewcontrollerdelegate? @IBAction func doneaction(_ sender) @IBAction func CancelAction(_ sender) }
6.UIActionSheet UIActionSheet UIActionSheetDelegate actionsheet(_ actionsheet:,clickedbuttonatindex:) ActionSheet (dismiss) actionsheet(_ actionsheet,diddismisswithbuttonindex:)
6.UIActionSheet UIActionSheet UIActionSheetDelegate actionsheet(_ actionsheet:,clickedbuttonatindex:) ActionSheet (dismiss) actionsheet(_ actionsheet,diddismisswithbuttonindex:)
6.UIActionSheet UIActionSheet UIActionSheetDelegate actionsheet(_ actionsheet:,clickedbuttonatindex:) ActionSheet (dismiss) actionsheet(_ actionsheet,diddismisswithbuttonindex:)
7.UIAlertView (Alert) UIAlertViewDelegate alertview(_ alertview, clickedbuttonatindex:)
7.UIAlertView (Alert) UIAlertViewDelegate alertview(_ alertview, clickedbuttonatindex:)
7.UIAlertView (Alert) UIAlertViewDelegate alertview(_ alertview, clickedbuttonatindex:)
UIAlertView UIActionSheet UIAlertView UIActionSheet Apple ios UI Element Usage Guidelines UIControlDemo
UIAlertController(iOS 8+) ios 8 引入 UIAlertController, 统一了 AlertView 及 ActionSheet 的使用 : 简单易用, 轻易在 alert view 及 action sheet 样式之间切换可配合闭包使用自适应, 在 ipad 下运行时自动以 popover 呈现
将样式改为 Alert 即成为 alert view 由于 ipad 环境下 action sheet 需要以 popover 形式呈现, 故应加上以下代码 : alert.popoverpresentationcontroller?.sourceview = view alert.popoverpresentationcontroller?.sourcerect = sender.frame
UIPageViewController UIPageViewController 是 ios 5 引入的新界面, 用于显示一系列 UIViewController 的场景 用户使用轻扫 (swipe) 动作在水平或垂直方向在各个场景间切换 ibooks 是典型的使用 UIPageViewController 的例子 Xcode 有 Page-Based Application 模版
UIPageViewController 页面切换样式 : enum UIPageViewControllerTransitionStyle : Int { } case PageCurl // Navigate between views via a page curl transition. case Scroll // Navigate between views by scrolling. PageCurl 切换样式且 doublesided = true
UIPageViewController 重要的属性 :
UIPageViewController 重要的属性 : var viewcontrollers: [AnyObject]! {get} 某时刻同时显示的内容控制器
UIPageViewController 重要的属性 : var viewcontrollers: [AnyObject]! {get} 某时刻同时显示的内容控制器 单页 viewcontrollers=[vc]
UIPageViewController 重要的属性 : var viewcontrollers: [AnyObject]! {get} 某时刻同时显示的内容控制器
UIPageViewController 重要的属性 : var viewcontrollers: [AnyObject]! {get} 某时刻同时显示的内容控制器 双页 viewcontrollers= [ vc1,vc2 ]
UIPageViewController 重要的属性 : var viewcontrollers: [AnyObject]! {get} 某时刻同时显示的内容控制器
UIPageViewController 重要的属性 : var viewcontrollers: [AnyObject]! {get} 某时刻同时显示的内容控制器 var delegate: UIPageViewControllerDelegate?
UIPageViewController 重要的属性 : var viewcontrollers: [AnyObject]! {get} 某时刻同时显示的内容控制器 var delegate: UIPageViewControllerDelegate?
UIPageViewController 重要的属性 : var viewcontrollers: [AnyObject]! {get} 某时刻同时显示的内容控制器 var delegate: UIPageViewControllerDelegate? var datasource: UIPageViewControllerDataSource
UIPageViewController 重要的属性 : var viewcontrollers: [AnyObject]! {get} 某时刻同时显示的内容控制器 var delegate: UIPageViewControllerDelegate? var datasource: UIPageViewControllerDataSource
UIPageViewController 重要的属性 : var viewcontrollers: [AnyObject]! {get} 某时刻同时显示的内容控制器 var delegate: UIPageViewControllerDelegate? var datasource: UIPageViewControllerDataSource var doublesided: Bool 页面切换时反面是否显示内容
UIPageViewController 重要的属性 : var spinelocation: UIPageViewControllerSpineLocation {get} spinelocation 书脊位置 仅对 PageCurl 有效 Min Max Mid
示例 :PageViewControl
UIViewController Containment ios 设备的屏幕越来越大, 分辨率也越来越高, 使得应用程序可将屏幕分割成多个区域, 如图所示 UIViewController Containment 特性可将个区域子视图所对应的视图控制器添加成根视图控制器的子控制器, 形成控制器的层次结构 (Hierarchy), 如此, 屏幕上发生的事件 ( 如手势 设备方向变化等 ) 可以由根视图控制器传递到各子视图控制器, 以便各子视图控制器对事件进行处理
UIViewController Containment func addchildviewcontroller(_ childcontroller: UIViewController) 根视图控制器调用, 将指定视图控制器添加为其子视图控制器 func removefromparentviewcontroller() 子视图控制器调用, 将自己从父视图控制器层次中移除 func transitionfromviewcontroller(_ fromviewcontroller, toviewcontroller,duration,options,animations,completion) 用一个视图替代另一个视图的方法, 或将一个子视图移到屏幕最前 func willmovetoparentviewcontroller(_ parent: UIViewController?) 调用 addchildviewcontroller 方法后会自动调用本方法, 可进行子控制器添加到父控制器之前需要的操作 func didmovetoparentviewcontroller(_ parent: UIViewController?) 在调用 addchildviewcontroller 方法后, 子视图控制器必须调用该方法
UIViewController Containment 程序设计 (adap)
UIViewController Containment 程序设计 (adap) 1. Storyboard
UIViewController Containment 程序设计 (adap) 1. Storyboard 在根视图控制器上放置若干 Container View 以及对应数目的控制器场景, 在 StoryBoard 中设置他们的所属关系即可
UIViewController Containment 程序设计 (adap) 1. Storyboard 在根视图控制器上放置若干 Container View 以及对应数目的控制器场景, 在 StoryBoard 中设置他们的所属关系即可 2. Code
UIViewController Containment 程序设计 (adap) 1. Storyboard 在根视图控制器上放置若干 Container View 以及对应数目的控制器场景, 在 StoryBoard 中设置他们的所属关系即可 2. Code
UIViewController Containment 程序设计 (adap) 1. Storyboard 在根视图控制器上放置若干 Container View 以及对应数目的控制器场景, 在 StoryBoard 中设置他们的所属关系即可 2. Code
附录 : 导航栏对象 http://www.gdou.com
http://www.gdou.com