【iOS 开发】iOS 开发 简介 (IOS项目文件 | MVC 模式 | 事件响应机制 | Storyboard 控制界面 | 代码控制界面 | Retina 屏幕图片适配)
一. iOS 項(xiàng)目簡介
1. iOS 文件簡介
創(chuàng)建一個(gè) HelloWorld 項(xiàng)目, 在這個(gè) IOS 項(xiàng)目中有四個(gè)目錄 : 如下圖;
-- HelloWorldTests 目錄 : 單元測試相關(guān)的類和資源;
(1) HelloWorld 目錄
HelloWorld 目錄介紹 :?
-- 命名規(guī)則 : 該目錄名稱與 IOS 項(xiàng)目名稱相同, 是主目錄;
-- 存放內(nèi)容 : IOS 項(xiàng)目的 源碼文件, 界面設(shè)計(jì)文件, 資源文件都存放在該目錄下;
-- 源文件 : Objective C 的 .m 和 .h 后綴的文件;
-- 界面設(shè)計(jì)文件 : Main.storyboard 是界面設(shè)計(jì)文件, 原來使用的是 nib 界面設(shè)計(jì)文件, storyboard 是新版本使用的;
-- Supporting Files : 支持庫, 一般保存 不是 Objective-C 的源代碼 和 資源文件;
Supporting Files 目錄簡介 :?
-- HelloWorld-info.plist 文件 : 文件名格式為 項(xiàng)目名-info.plist, 屬性列表文件, 保存 IOS 項(xiàng)目的各種信息;
-- InfoPlist.strings 文件 : 該文件保存字符串, 主要為程序提供國際化支持;
-- main.m 文件 : main() 函數(shù)程序入口文件;
-- HelloWorld-Prefix.pch 文件 : 項(xiàng)目中用到的來自外部框架的頭文件在這里面注明, Xcode 會(huì)預(yù)編譯這些文件內(nèi)容;
(2) Frameworks 目錄
Frameworks 目錄簡介 :?
-- 包含文件 : 包含項(xiàng)目所依賴的框架 或者 庫, 還有 聲音 圖像 等資源文件也可以放在該文件中;
-- 自動(dòng)鏈接 : 框架和庫一旦被添加到該目錄后, 會(huì)自動(dòng)鏈接到應(yīng)用中, IOS 應(yīng)用可以調(diào)用這些資源 和 庫中的類 和 函數(shù);
-- 系統(tǒng)默認(rèn)框架 : Foundation.framework, CoreGraphics.framwork, UIKit.framework, CoreData.framework, XCTest.framework 五個(gè)默認(rèn)框架;
(3) Products 目錄
Products 目錄 :?
-- 包含內(nèi)容 : 包含該項(xiàng)目所生成的應(yīng)用程序, 其中 HelloWorld.app 就是安裝文件;
-- 紅色顯示 : 由于還沒有編譯程序, 該文件顯示紅色, 這說明該文件目前不存在;
(4) 目錄對(duì)應(yīng)
Xcode 與 文件系統(tǒng) :?
-- Xcode 邏輯分組 :?
-- 真實(shí)文件系統(tǒng) :?
localhost:HelloWorld octopus$ tree . |____.DS_Store |____Base.lproj | |____.DS_Store | |____Main.storyboard |____en.lproj | |____.DS_Store | |____InfoPlist.strings |____HelloWorld-Info.plist |____HelloWorld-Prefix.pch |____HelloWorld.xcdatamodeld | |____.xccurrentversion | |____HelloWorld.xcdatamodel | | |____contents |____Images.xcassets | |____.DS_Store | |____AppIcon.appiconset | | |____Contents.json | |____LaunchImage.launchimage | | |____Contents.json |____main.m |____OCTAppDelegate.h |____OCTAppDelegate.m |____OCTDetailViewController.h |____OCTDetailViewController.m |____OCTMasterViewController.h |____OCTMasterViewController.m
-- 不對(duì)應(yīng) : Xcode 導(dǎo)航面板中的目錄 文件 與 真實(shí)文件系統(tǒng)中的目錄文件不是對(duì)應(yīng)的;
-- 邏輯分組 : Xcode 將 真實(shí)文件進(jìn)行了邏輯分組, 并不是真實(shí)的文件結(jié)構(gòu);
-- 自定義分組 : 開發(fā)者還可以創(chuàng)建自定義的分組;
mac 中的 tree 命令 :?
-- tree 命令模擬 :?
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'-- 模擬 tree 命令 : 之后可以調(diào)用 tree 命令了;
alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
2. Interface Builder 簡介
(1) 刪除 Main.storyboard 文件
刪除 Main.storyboard 文件 :?
-- 刪除方法 : 選擇文件, 按 Delete 鍵, 彈出以下對(duì)話框, 選擇 Move to Trash 選項(xiàng), 直接刪除;
-- Remove Reference 選項(xiàng) : 在文件系統(tǒng)保留該文件, 但是不編譯該文件;
-- 修改配置 : 點(diǎn)擊項(xiàng)目名稱, 選擇 General 面板, 然后刪除 Main Interface 中的 Main 選項(xiàng);
(2) 創(chuàng)建 xib 界面設(shè)計(jì) 文件
創(chuàng)建 xib 文件流程 :?
-- 選擇文件類型 : 菜單欄選擇 File -> New -> File, 彈出下面對(duì)話框, 選擇 iOS 的 User Interface 選項(xiàng)卡中的 View 選項(xiàng), 點(diǎn)擊 Next 按鈕;?
-- 創(chuàng)建文件 : 輸入名稱 Main.xib 自定義的, 選擇分組 HelloWorld, 選擇所輸出的 app 文件;
-- 創(chuàng)建成功 :?
(3) xib 界面設(shè)計(jì) 文件 簡介
xib 界面設(shè)計(jì)文件 :?
-- PlaceHolder 列表區(qū) : 包括 File's Owner 和 First Responder 兩個(gè)文件;
-- Objects 列表區(qū) : 保存界面布局文件中的 UI 控件實(shí)例;
PlaceHolder 列表區(qū) :?
-- File's Owner 文件 : 代表加載界面設(shè)計(jì)文件對(duì)象, 即 布局文件對(duì)象, 誰負(fù)責(zé)加載 Main.xib 界面設(shè)計(jì)文件, 誰就是該 界面設(shè)計(jì)文件的 File's Owner;
-- First Responder 文件 : 代表正在與用戶交互的對(duì)象, 即獲取到焦點(diǎn)的對(duì)象, 其代表的控件會(huì)變化, 用戶點(diǎn)擊按鈕時(shí), 按鈕就成為了其代表的控件;
Objects 列表區(qū) :?
-- 主要作用 : 該列表區(qū) 保存 xib 界面 所用到的 UI 控件實(shí)例;
-- 工作流程 : 開發(fā)者將 UI 控件 拖入 xib 文件, app 運(yùn)行時(shí)會(huì)加載 xib 文件, 并為每個(gè)控件都創(chuàng)建對(duì)象, Objects 列表區(qū)可以看到所有的 UI 控件;
(4) xib 和 nib 文件
xib 和 nib 后綴文件 :?
-- 新舊區(qū)別 : nib 后綴是傳統(tǒng)格式, xib 后綴是新格式;
-- 默認(rèn)模板 : Xcode 默認(rèn)使用 xib 作為默認(rèn)格式;
xib 與 nib 主要區(qū)別 :
-- 文件類型 : xib 是文本文件, nib 是二進(jìn)制文件, Xcode 編譯項(xiàng)目時(shí) 會(huì)將 xib 文件轉(zhuǎn)為 nib 文件;?
-- nib 文件 : Interface Builder 把窗口, 菜單欄 以及 各種 UI 組件都凍結(jié)在 nib 文件中, 使用 nib 界面設(shè)計(jì)文件, 這些 UI 控件對(duì)象已經(jīng)被創(chuàng)建好, app 運(yùn)行時(shí), 只需要蘇醒這些 UI 控件 即可;
-- xib 文件優(yōu)點(diǎn) : xib 更方便, 便于版本控制;
3. nib 文件添加控件
xib 添加控件 :?
-- 對(duì)象庫面板快捷鍵 : 使用 control + option + command + 1 快捷鍵, 即可顯示對(duì)象庫面板;
-- 添加 Label 控件 到界面中 : Label 拖到中間會(huì)有 水平 和 垂直兩條 藍(lán)色引導(dǎo)線;
-- 邊界引導(dǎo)線 : 如果把控件拖到邊界, 會(huì)出現(xiàn)邊界引導(dǎo)線, 說明不能再向外拖了;
上 :?? 左 : ??右 :? ? ?
下 :?
-- 顯示 UI 控件輪廓 : Editor -> Canvas -> Show Layout Rectangles, 可以看到布局出現(xiàn)下面的輪廓;
4. 控件屬性修改相關(guān)面板
相關(guān)面板 :?
-- 文件檢查器面板 : 快捷鍵 option + command + 1, 文件相關(guān)信息;
-- 快速幫助檢查器面板 : 快捷鍵 option + command + 2, 顯示先關(guān)的幫助文檔;
-- 身份檢查器面板 :?快捷鍵 option + command + 3, 管理界面控件的 實(shí)現(xiàn)類, 恢復(fù) ID 等屬性;
-- 屬性檢查器面板 :快捷鍵 option + command + 4, 管理控件的 拉伸方式, 背景色 等外觀屬性;
-- 大小檢查器面板 :?快捷鍵 option + command + 5, 管理控件 大小 位置 相關(guān)屬性;
-- 連接檢查器面板 : ?快捷鍵 option + command + 6, 管理控件 與 代碼之間關(guān)聯(lián);
4. UIView 屬性
UIView 屬性?:?
(1) Mode 屬性
Mode 屬性 :
-- 主要作用 : 用于控制 UI 控件內(nèi)圖片的對(duì)齊方式, 是否縮放適配控件;
-- Scale To Fit : 縮放圖片, 保證圖片適配該控件;
-- Aspect Fit : 保持縱橫比縮放圖片, 保證圖片能夠適應(yīng)控件;
-- Aspect Fill : 保持縱橫比縮放圖片, 保證圖片能夠填充控件;
-- 注意 : 縮放會(huì)帶來不必要的開銷, 最好為不同屏幕尺寸創(chuàng)建對(duì)應(yīng)的圖片;
(2) Tag 屬性
Tag 屬性 : 該屬性是 控件的 唯一標(biāo)識(shí), 可以根據(jù)該 標(biāo)識(shí) 獲取該 UI 控件的引用;
(3) Interaction 屬性
Interaction 屬性 :?
-- User Interface Enable : 勾選, 表明該控件支持與用戶交互;
-- Multiple Touch : 勾選, 表明該控件支持多點(diǎn)觸控, 支持更復(fù)雜的手勢;
(4) Alpha 屬性
Alpha 屬性 :?
-- 作用 : 用于控制該控件的透明度, 其屬性支持 0.0 ~ 1.0 任意浮點(diǎn)數(shù);
-- 注意 : 如果設(shè)置為半透明效果, 會(huì)增加額外開銷;
(5) Background 屬性
background 屬性 :?
-- 作用 : 設(shè)置控件的背景色;
(6) Drawing 屬性
Drawing 屬性 :?
-- Opaque : 設(shè)置控件是否為 不透明, 如果設(shè)置為不透明, 不管該控件的透明度是否是半透明, 該控件后的其它控件將不會(huì)被繪制;
-- Hidden : 隱藏該控件, 用戶看不到本控件;
-- Clears Graphics Context : 清除該控件所覆蓋的區(qū)域, 勾選該選項(xiàng)后 IOS 會(huì)清除該控件覆蓋的區(qū)域, 然后繪制該控件, 會(huì)降低性能;
-- Clip Subviews : 是否裁剪子控件, 當(dāng)該控件包含多個(gè)子控件, 并且這些子控件超出了父類容器, 勾選后, 超出部分就不會(huì)被繪制;
-- Autoresize Subviews : 是否自動(dòng)調(diào)整子控件大小, 如果勾選, 該控件大小改變時(shí), 其子控件也會(huì)隨之調(diào)整大小;
(7) Streching 屬性
Streching 屬性 :
-- 主要作用 : 用于控制 UIView 拉伸區(qū)域, 只有在需要調(diào)整控件大小并重繪控件時(shí)才需要定義拉伸區(qū)域;
5. UILabel 屬性
UILabel 屬性 :?
(1) Text 屬性
Text 屬性 :?
-- 屬性值 : 支持 Plain 和 Attribute 屬性, 一般設(shè)置為 Plain;
-- 文本 : 在 Text 屬性下面有一個(gè)文本框, 在這個(gè)文本框中輸入要顯示的文本內(nèi)容;
(2) Color 屬性
Color 屬性 : 控制 UILabel 中的文本顏色;
(3) Font 屬性
Font 屬性 :?
?
-- 作用 : 控制文本 字體 文字大小 風(fēng)格等屬性;
-- System : 系統(tǒng)默認(rèn)字體, 不能修改 Family Style Size 屬性;
-- System Bold : 系統(tǒng)默認(rèn)的粗體字;
-- System Italic : 系統(tǒng)默認(rèn)的斜體字;
(4) Alignment 屬性
Alignment 屬性 :?
-- 作用 : 設(shè)置對(duì)齊方式, 左對(duì)齊, 居中對(duì)齊, 右對(duì)齊;
(5) Lines 屬性
Lines 屬性 :?
-- 作用 : 控制 文本行數(shù);
(6) Behavior 屬性
Behavior 屬性 :?
-- Enabled : 控制 UILabel 是否可用, 如果沒有勾選, 該 Label 不可用, 處于淡灰色;
-- Highlighted : 控制 UILabel 是否處于高亮狀態(tài), 勾選該控件后處于高亮狀態(tài), 控件中的文本以高亮顯示;
(7) Line Break 屬性
Line Break 屬性 :?
-- 作用 : 該屬性對(duì) UILabel 內(nèi)的文本進(jìn)行截?cái)? UILabel 中的字符串比較多, UILabel 無法容納這些字符串, 可以將字符串截?cái)? 顯示一部分;
-- Truncate Head : 顯示后半部分字符串, 前半部分使用 ... 代替;
-- Truncate Middle : 顯示 前后 部分, 中間使用 .. 替代;
-- Truncate Tail : 截?cái)嗪蟀氩糠? 后半部分使用 ... 替代;
(8) AutoShrink 屬性
AutoShrink 屬性 :?
-- 作用 : UILabel 中字符串較多 以至于 UILabel 無法容納字符串時(shí), AutoShrink 屬性控制文本自動(dòng)收縮;
-- Fixed Font Size : 不縮放;
-- Minimum Font Scale : 設(shè)置最小的縮放倍數(shù), 設(shè)置的值是 0.0 ~ 1.0 之間的浮點(diǎn)數(shù);
-- Minimum Font Size : 設(shè)置最小的字號(hào)大小, 設(shè)置的值是字體大小;
-- Tighten Letter Spacing : 勾選該復(fù)選框 會(huì)自動(dòng)壓縮字符串中的空白, 保證 UILabel 能容納這些文本中的內(nèi)容;
(9) Highlighted 屬性
Highlighted 屬性 :?
-- 作用 : 為 UILabel 中的文本設(shè)置高亮顯示, 前提 Behavior 屬性的 Highlighted 屬性勾選;
(10) Shadow 屬性
Shadow 屬性 :?
-- 作用 : 為文本設(shè)置陰影顏色;
(11) Shadow Offset 屬性
Shadow Offset 屬性 :?
-- 作用 : 控制陰影文本與正常文本之間的偏移量,?
-- Horizontal : 屬性值 >0 向右偏移, < 0 向左偏移;
-- Vertical : 屬性值 >0 向下偏移, <0 向上偏移;
二. IOS 中的 MVC 框架
1. 程序入口
(1) 程序入口代碼
程序入口代碼示例 :?
// // main.m // FirstApp // // Created by octopus on 15-11-4. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved. //#import <UIKit/UIKit.h>#import "OCAppDelegate.h"int main(int argc, char * argv[]) {@autoreleasepool {return UIApplicationMain(argc, argv, nil, NSStringFromClass([OCAppDelegate class]));} }
-- Main 函數(shù)執(zhí)行流程 :?調(diào)用 UIApplicationMain () 函數(shù), 創(chuàng)建 UIApplication 對(duì)象, 為 UIApplication 對(duì)象設(shè)置委托, 負(fù)責(zé)處理應(yīng)用程序事件循環(huán);
2. 應(yīng)用程序委托
(1) 委托對(duì)象簡介
委托對(duì)象簡介 :?
-- UIApplication 本質(zhì) : UIApplication 代表了 IOS 應(yīng)用程序, 每個(gè)應(yīng)用中只有一個(gè) UIApplication;
-- 實(shí)際編程對(duì)象 : 雖然 UIApplication 代表了整個(gè)應(yīng)用, 但是編程時(shí)不直接操作 UIApplication, 開發(fā)者一般與 delegate 委托對(duì)象交互;
-- 全局變量 : UIApplication 委托對(duì)象全局可用, 定義全局變量即 將該變量設(shè)置為 UIApplication 的委托對(duì)象;
-- 數(shù)據(jù)交換 : 獲取委托對(duì)象, 可以向其中存取數(shù)據(jù), 可以作為 IOS 應(yīng)用的通信中心, 程序組件之間通過委托對(duì)象進(jìn)行數(shù)據(jù)交互;
-- 處理生命周期事件 : 應(yīng)用程序委托對(duì)象還處理 IOS 應(yīng)用的各種生命周期事件, 處理應(yīng)用執(zhí)行過程中的事件循環(huán);
(2) AppDelegate.h 詳解
OCAppDelegate.h 詳解 :?
-- 代碼示例 :?
// // OCAppDelegate.h // FirstApp // // Created by octopus on 15-11-4. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved. //#import <UIKit/UIKit.h>@interface OCAppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end
-- 繼承父類 : 該 OCAppDelegate 類, 繼承了 UIResponder 類, 表明該類可以作為響應(yīng)者使用;
-- 實(shí)現(xiàn)協(xié)議 : 實(shí)現(xiàn)了 UIApplicationDelegate 協(xié)議, 繼承這個(gè)協(xié)議即表明該對(duì)象是應(yīng)用程序委托類;
-- UIWindow 屬性 : 代表 IOS 應(yīng)用程序的窗口, 一個(gè)應(yīng)用只需創(chuàng)建一次 UIWindow 窗口;
-- FKViewController 屬性 : 代表視圖控制器, 系統(tǒng)創(chuàng)建控制器時(shí), 通常會(huì)加載對(duì)應(yīng)的 xib 文件;
-- 應(yīng)用初始界面 : 在 OCAppDelegate 中定義了 FKViewController 變量, 該變量類型對(duì)應(yīng)的 xib 文件就是初始界面;
(3) AppDelegate.m 詳解
AppDelegate.m 詳解 :?
-- 代碼示例 : 書中的示例代碼;
// // FKAppDelegate.m // HelloiOS // // Created by yeeku on 13-5-16. // Copyright (c) 2013年 crazyit.org. All rights reserved. //#import "FKAppDelegate.h"#import "FKViewController.h"@implementation FKAppDelegate // 當(dāng)應(yīng)用程序啟動(dòng)后將會(huì)調(diào)用該方法 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// 創(chuàng)建UIWindow對(duì)象,并初始化該窗口的大小與主屏幕大小相同// 程序?qū)?chuàng)建的UIWindow對(duì)象賦值給該程序委托對(duì)象的window屬性self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];// 創(chuàng)建FKViewController對(duì)象、并使用FKViewController界面布局文件來// 初始化該視圖控制器關(guān)聯(lián)的用戶界面self.viewController = [[FKViewController alloc] initWithNibName:@"FKViewController" bundle:nil];// 讓該程序的窗口加載、并顯示viewController視圖控制器關(guān)聯(lián)的用戶界面self.window.rootViewController = self.viewController;// 將該UIWindow對(duì)象設(shè)為主窗口、并顯示出來[self.window makeKeyAndVisible];return YES; }- (void)applicationWillResignActive:(UIApplication *)application {// 當(dāng)應(yīng)用程序從活動(dòng)狀態(tài)轉(zhuǎn)入不活動(dòng)狀態(tài)時(shí),系統(tǒng)將會(huì)調(diào)用該方法。// 通常來說的,當(dāng)應(yīng)用程序突然被中斷(比如有電話、短信進(jìn)來時(shí)),系統(tǒng)將會(huì)回調(diào)該方法// 另外,當(dāng)用戶離開該程序,程序開始轉(zhuǎn)入后臺(tái)狀態(tài)時(shí)也會(huì)回調(diào)該方法。 }- (void)applicationDidEnterBackground:(UIApplication *)application {// 通常可通過重寫該方法來釋放共享資源、保存用戶數(shù)據(jù),取消定時(shí)器。// 開發(fā)者還可通過該方法來保存足夠的狀態(tài)數(shù)據(jù),// 這樣保證用戶重新啟動(dòng)該應(yīng)用時(shí)能正確恢復(fù)到當(dāng)前狀態(tài)// 如果該應(yīng)用程序支持后臺(tái)執(zhí)行,當(dāng)用戶退出時(shí),系統(tǒng)調(diào)用調(diào)用該方法,而不是調(diào)用// applicationWillTerminate:方法 }- (void)applicationWillEnterForeground:(UIApplication *)application {// 當(dāng)應(yīng)用程序?qū)⒁M(jìn)入前臺(tái)時(shí)將會(huì)調(diào)用該方法。 }- (void)applicationDidBecomeActive:(UIApplication *)application {// 當(dāng)應(yīng)用程序進(jìn)入前臺(tái)、并轉(zhuǎn)入活動(dòng)狀態(tài)時(shí)將會(huì)調(diào)用該方法。 }- (void)applicationWillTerminate:(UIApplication *)application {// 當(dāng)應(yīng)用程序被終止時(shí),系統(tǒng)將會(huì)調(diào)用該方法。 }@end
-- 重寫 application 方法 : 應(yīng)用程序加載時(shí)會(huì)回調(diào)該方法;
application 方法流程 :?
-- 創(chuàng)建 UIWindow 對(duì)象 : 創(chuàng)建 UIWindow 對(duì)象, 該對(duì)象就是 IOS 應(yīng)用程序的窗口;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
-- 創(chuàng)建 ViewController 對(duì)象 : 該對(duì)象用于加載該對(duì)象對(duì)應(yīng)的 xib 等界面設(shè)計(jì)文件;
self.viewController = [[FKViewController alloc] initWithNibName:@"FKViewController" bundle:nil];
-- 顯示界面 : 讓應(yīng)用程序窗口顯示 ViewController 對(duì)象;
self.window.rootViewController = self.viewController;
-- 主窗口 : 將該程序的窗口設(shè)置為主窗口, 將主窗口顯示出來;
[self.window makeKeyAndVisible];
3. IOS 的 MVC 模式 -?M(Model 模型) V (View 視圖) C (Controller 控制器)
IOS MVC 模式 :?
-- Model 模型 : Model 一般式普通的 Objective - C 類, 這些類用于保存 處理 數(shù)據(jù);
-- View 視圖 : 使用 Interface Builder 創(chuàng)建的視圖組件, 如 xib, storyboard 后綴的界面設(shè)計(jì)文件;
-- Controller 控制器 : 一般是 Objective - C 類, 一般都會(huì)繼承 UIViewController 基類;
4. UIViewController 控制器
UIController 方法詳解 : 主要用于處理視圖加載, 顯示等事件;
-- viewDidLoad 方法 : 視圖裝載完成后, 自動(dòng)回調(diào)該方法, 重寫時(shí)注意先使用 [super viewDidLoad] 把父類方法調(diào)用了, 在其后追加自己處理的內(nèi)容;
-- didReceiveMemoryWarning 方法 : 系統(tǒng)自動(dòng)回調(diào)的, 內(nèi)存緊張時(shí)會(huì)調(diào)用此方法, 在方法內(nèi)釋放內(nèi)存, 注意先調(diào)用 [super didReceiveMemoryWarning] 方法;
-- viewWillAppear 方法 : 視圖將要顯示時(shí)系統(tǒng)會(huì)自動(dòng)回調(diào)該方法;
-- viewDidAppear 方法 : 視圖顯示出來后系統(tǒng)自動(dòng)回調(diào)的方法;
-- viewWillDisappear 方法 : 視圖將要被隱藏 或者 關(guān)閉時(shí), 回調(diào)該方法;
-- viewDidDisappear 方法 : 視圖被隱藏 或 關(guān)閉后, 回調(diào)該方法;
-- viewWillLayoutSubviews 方法 : 視圖將要排列其子視圖時(shí), 需要回調(diào)的方法;
-- viewDidLayoutSubviews 方法 : 視圖已經(jīng)排列好其子視圖后, 回調(diào)的方法;
UIViewController 代碼示例 :?
-- 頭文件 :?
// // OCViewController.h // FirstApp // // Created by octopus on 15-11-4. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved. //#import <UIKit/UIKit.h>@interface OCViewController : UIViewController@property (strong, nonatomic) IBOutlet UILabel *label1; @property (strong, nonatomic) IBOutlet UILabel *label2; @property (strong, nonatomic) IBOutlet UIButton *button; - (IBAction)button_click:(id)sender;@end
-- 實(shí)現(xiàn)文件 :?
// // OCViewController.m // FirstApp // // Created by octopus on 15-11-4. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved. //#import "OCViewController.h"@interface OCViewController ()@end@implementation OCViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.//使用 IBOutlet 獲取 UI 控件[self.label1 setText:@"Tom"];//使用 tag 獲取 UI 控件UILabel * label_tag = (UILabel *)[self.view viewWithTag:1];[label_tag setText:@"Jerry"]; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }- (IBAction)button_click:(id)sender {//使用 IBOutlet 獲取 UI 控件[self.label1 setText:@"John"];//使用 tag 獲取 UI 控件UILabel * label_tag = (UILabel *)[self.view viewWithTag:1];[label_tag setText:@"John"]; } @end
三. IOS 中的 事件機(jī)制
1. 通過 IBOutlet 連接獲取控件
(1) IBoutlet 屬性
IBOutlet 屬性 :?
-- 主要作用 : IBOutlet 屬性可以連接 Objective - C 源碼 和 界面設(shè)計(jì)文件 (.xib 文件 或 .storyboard 文件);
-- 連接方法 : 在界面設(shè)計(jì)文件對(duì)應(yīng)的 UIViewController 類中, 定義一個(gè) IBOutlet 屬性, 該屬性 與 Interface Builder 中的控件相關(guān)聯(lián);
-- 調(diào)用方法 : 在 UIViewController 中調(diào)用 控件的方法, 使用 IBOutlet 屬性即可訪問 界面設(shè)計(jì)文件的組件;
(2) IBoutlet 連接步驟
IBOutlet 連接方法 :?
-- 打開輔助編輯器 :?
-- 輔助編輯器界面 : 左邊顯示 界面設(shè)計(jì)文件, 右邊顯示 UIViewController 類;
-- 拖動(dòng)控件到 UIViewController 中 : 按住 control 鍵, 將控件拖到 UIViewController.h 中;
-- 設(shè)置 IBOutlet 的屬性 : 控件拖到 UIViewController 中后, 彈出下面的對(duì)話框;
-- 連接控件 : 點(diǎn)擊上面的 Connect, 在 UIViewController 中就會(huì)出現(xiàn) @property IBOutlet 屬性;
// // OCViewController.h // FirstApp // // Created by octopus on 15-11-4. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved. //#import <UIKit/UIKit.h>@interface OCViewController : UIViewController@property (strong, nonatomic) IBOutlet UILabel *label1; @property (strong, nonatomic) IBOutlet UILabel *label2; @property (strong, nonatomic) IBOutlet UIButton *button; - (IBAction)button_click:(id)sender;@end
(3) IBoutlet 屬性解析
IBOutlet 屬性解析 :?
-- Connection : 設(shè)置該控件的 IBOutlet 或 IBOutlet 集合, 默認(rèn) IBOutlet 屬性;
-- Name : 屬性名, 即 @property 的屬性名;
-- Type : 界面控件的類型, UILabel 等控件名稱;
-- Storage : Weak, Strong 指針的強(qiáng) 弱 存儲(chǔ)機(jī)制;
(4) IBoutlet 關(guān)聯(lián)查看
IBOutlet 屬性 :?
-- 不使用 @synthesize : 使用 @property 定義的屬性, 在 UIViewController.m 中不需要 使用 @synthesize 設(shè)置 setter getter 方法;
-- 查看連接關(guān)系 : 使用 Xcode 的連接檢查器面板查看 IBOutlet 屬性 與 UI控件 間關(guān)聯(lián), 使用 control + option + command + 6 或者 右上角的 ?圖標(biāo)?, 注意 要在界面設(shè)計(jì)文件中先選擇 File Owner;
解析連接器面板中的 IBOutlet 元素 :?
-- Outlets : 多個(gè) IBOutlet 屬性, 每個(gè)屬性都對(duì)應(yīng) 界面設(shè)計(jì)文件中的一個(gè) UI 控件;
-- Referencing Outlets : 代表被選擇對(duì)象本身被關(guān)聯(lián)到其他對(duì)象的 IBOutlet 屬性;
-- Referencing Outlets Collections : 控件被關(guān)聯(lián)到其它對(duì)象的 IBOutlet 集合屬性;
2. 通過 Tag 屬性獲取控件
(1) Tag 獲取屬性簡介
通過 Tag 屬性獲取控件流程 :?
-- 設(shè)置 UILabel 控件的 Tag 屬性 : Command + option + 4 即可顯示屬性界面;
-- 獲取 UILabel 控件 : 通過 viewWithTag : tag標(biāo)簽 獲取 UI 控件的引用;?
//使用 tag 獲取 UI 控件UILabel * label_tag = (UILabel *)[self.view viewWithTag:1];[label_tag setText:@"Jerry"];
3. IBAction 綁定實(shí)現(xiàn)事件處理
(1) IBAction 簡介
IBAction 簡介 :?
-- 主要作用 : IBAction 是 UIViewController 中的特殊方法, 這該方法是 UI 控件事件處理方法;
IBAction 事件綁定流程 :?
-- 需求 : 為UIButton 組件添加點(diǎn)擊方法;?
-- 創(chuàng)建 UI 組件 : Interface Builder 中創(chuàng)建 UIButton 對(duì)象;
-- 創(chuàng)建 IBAction 方法 : 在 界面設(shè)計(jì)文件 對(duì)應(yīng)的 UIViewController 中定義 IBAction 方法;
-- 綁定 IBAction 方法與 UI 組件 : 將 UIViewController 中的 IBAction 方法 與 界面設(shè)計(jì)文件中的 UIButton 組件綁定;
-- 觸發(fā) IBAction 方法 :?界面中的 UIButton 被點(diǎn)擊, 就會(huì)回調(diào) IBAction 方法;
(2) IBAction 綁定步驟
IBAction 綁定步驟 :?
-- 選中界面設(shè)計(jì)文件, 打開輔助編輯器 :?
-- 拖動(dòng)按鈕到 UIViewController.h 文件中 : 按住 control 鍵不放, 將按鈕拖動(dòng)到 UIViewController 中;
-- 設(shè)置 IBAction 屬性 : 在接下來彈出的對(duì)話框中的 Connection 列表設(shè)置 為 Action, 將設(shè)置 IBAction 屬性, 設(shè)置完畢后會(huì)自動(dòng)生成點(diǎn)擊事件回調(diào)方法, 之后在實(shí)現(xiàn)類中實(shí)現(xiàn)這個(gè)方法即可;
(3) IBAction 屬性詳解
IBAction 屬性 :?
-- Connection : 選擇為 UI 控件創(chuàng)建 IBOutlet 還是 IBAction 屬性類型;
-- Name : IBAction 方法名;
-- Type : 觸發(fā)該事件的 UI 控件類型, 默認(rèn)為 id, 如果需要更精準(zhǔn)可以設(shè)置成具體的 UI 組件類, 如 UIButton;
-- Event : 選擇為什么樣的事件綁定 IBAction 屬性, 默認(rèn) Touch Up Inside 在 UI 控件區(qū)域按下并松開;
-- Arguments : 選擇 IBAction 方法形參列表, Sender 觸發(fā) IBAction 方法的事件源, Sender And Event, Event 是事件本身;
4. 通過代碼設(shè)置事件處理方法
(1) 注冊(cè)刪除事件響應(yīng)方法
注冊(cè)刪除事件響應(yīng)方法 :?
-- 注冊(cè)刪除事件方法來源 : IOS 中與用戶交互的控件都繼承了 UIControl 類, 注冊(cè) 刪除 控件響應(yīng)方法 是該 UIControl 基類提供的;
-- 添加事件響應(yīng)方法 : "addTarget : action : forControlEvents :" , 為當(dāng)前控件的 指定事件 設(shè)置 處理方法;
--?移除事件響應(yīng)方法 : "removeTarget : action : forControlEvent : " , 刪除 UIControl 指定的事件處理方法.
(2) 注冊(cè)刪除事件響應(yīng)方法參數(shù)解析
添加移除事件方法參數(shù)解析 :?
-- addTarget / removeTarget 參數(shù) : 指定 事件響應(yīng)方法 所在的對(duì)象;
-- action 參數(shù) : 接受一個(gè) SEL 參數(shù), 該 參數(shù)代表的方法作為事件處理方法;
-- forControlEvents 參數(shù) : UIControlEvent 枚舉值參數(shù), 用于指定事件類型.
(3) 代碼示例
代碼示例 :?
-- OCViewController.h 頭文件 : 聲明 IBOutlet 指定 UI 控件引用;
@property (strong, nonatomic) IBOutlet UIButton *UIControl_button;
-- OCViewController.m 文件 : 為 UI 控件添加事件監(jiān)聽;
//注意 selector 中的方法, 如果后面有參數(shù) 則需要帶上 冒號(hào) ":"[self.UIControl_button addTarget: self action:@selector(button_click2:) forControlEvents: UIControlEventTouchUpInside];
-- 添加的方法 :?
- (void) button_click2:(id)sender{[self.label1 setText:@"octopus"];[self.label2 setText:@"octopus"];}
四. StoryBoard 介紹
1. StoryBoard 簡介
StoryBoard 簡介 :?
-- 使用版本 : IOS 5 之后默認(rèn)使用 storyboard 作為界面設(shè)計(jì)文件, 而之前是 xib 作為默認(rèn)的界面設(shè)計(jì)文件;
-- 與 xib 界面設(shè)計(jì)文件的共同點(diǎn) : 項(xiàng)目中主要包含 應(yīng)用委托類, 界面設(shè)計(jì)文件, 界面控制類, 如下圖 :?
-- 不同點(diǎn) : UIViewController 與界面設(shè)計(jì)文件的名稱不再相同, 界面設(shè)計(jì)文件名稱是 Main.storyboard, 該界面設(shè)計(jì)文件包含所有的界面;
-- 界面設(shè)計(jì)文件 與 界面控制類對(duì)應(yīng)關(guān)系 : 一個(gè) storyboard 界面設(shè)計(jì)文件 對(duì)應(yīng) 多個(gè) 界面控制類;
2. StoryBoard 與 xib 區(qū)別詳解
(1) Scence 概念
Scene 場景簡介 :?
-- Scene 組成 : Scene 由 整個(gè)視圖, 視圖控制器, 第一響應(yīng)者 組成;
-- 場景與界面 : 一個(gè)場景 對(duì)應(yīng) 一個(gè)窗口界面;?
-- 層次關(guān)系 : View Controller 在 Scene 中, UI 控件 在 View Controller 中, 如下圖 :?
(2) 灰色箭頭
灰色箭頭作用 :?
-- 作用 : 箭頭表示 UI 控制器之間的切換關(guān)系, 箭頭指向需要加載的 視圖控制器;
-- 移動(dòng)箭頭 : 視圖中只有一個(gè) UI 控制器, 移動(dòng)箭頭始終指向唯一的 一個(gè) UI 控制器;
-- 好處 : 可以直觀地看到多個(gè)視圖之間的協(xié)作關(guān)系, 可以在 Interface Builder 中配置視圖之間的切換關(guān)系;
-- 圖示 :?
(3) 工具圖標(biāo)
界面設(shè)計(jì)文件的 工具圖標(biāo) :?
-- 縮小 : 多個(gè)視圖控制器在同一個(gè)窗口, 縮小界面可以同時(shí)查看所有的 視圖控制器;
-- 恢復(fù) : 恢復(fù)原樣查看;
-- 放大 : 放大界面設(shè)計(jì)文件;
3. AppDelegate 委托類對(duì)比
委托類代碼對(duì)比 :?
-- xib 委托類 : 加載完成方法;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// 創(chuàng)建UIWindow對(duì)象,并初始化該窗口的大小與主屏幕大小相同// 程序?qū)?chuàng)建的UIWindow對(duì)象賦值給該程序委托對(duì)象的window屬性self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];// 創(chuàng)建FKViewController對(duì)象、并使用FKViewController界面布局文件來// 初始化該視圖控制器關(guān)聯(lián)的用戶界面self.viewController = [[FKViewController alloc] initWithNibName:@"FKViewController" bundle:nil];// 讓該程序的窗口加載、并顯示viewController視圖控制器關(guān)聯(lián)的用戶界面self.window.rootViewController = self.viewController;// 將該UIWindow對(duì)象設(shè)為主窗口、并顯示出來[self.window makeKeyAndVisible];return YES; }
-- storyboard 委托類 : 加載完成方法;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.return YES; }
-- 主要區(qū)別 : storyboard 不需要?jiǎng)?chuàng)建 UIViewWindow ?和 UIViewController, 不需要將 UIViewController 添加到 UIWindow 中;
-- 設(shè)置 storyboard : 點(diǎn)擊 項(xiàng)目選項(xiàng) , 在 General 選項(xiàng)卡中, 選擇左側(cè) TARGETS, 選中對(duì)應(yīng)項(xiàng)目, 在 Main Interface 中選擇 storyboard 界面設(shè)計(jì)文件;
五. 代碼控制 UI 界面
1. 代碼生成 UI 界面
(1) 代碼生成 UI 界面示例
基本無界面 UI 顯示代碼示例 :?
-- 創(chuàng)建 Empty Application :?
-- OCAppDelegate.h :?
// // OCAppDelegate.h // Empty // // Created by octopus on 15-11-19. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved. //#import <UIKit/UIKit.h>@interface OCAppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UILabel * label;@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;- (void)saveContext; - (NSURL *)applicationDocumentsDirectory;@end
-- OCAppDelegate.m :?
// // OCAppDelegate.m // Empty // // Created by octopus on 15-11-19. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved. //#import "OCAppDelegate.h"@implementation OCAppDelegate@synthesize managedObjectContext = _managedObjectContext; @synthesize managedObjectModel = _managedObjectModel; @synthesize persistentStoreCoordinator = _persistentStoreCoordinator; @synthesize label;- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// 創(chuàng)建 UIWindow 對(duì)象, 設(shè)置其大小與屏幕大小相同self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];// 設(shè)置背景顏色self.window.backgroundColor = [UIColor whiteColor];// 創(chuàng)建 UIViewController 對(duì)象UIViewController * controller = [[UIViewController alloc] init];// 窗口加載并顯示 視圖控制器 并 關(guān)聯(lián) 用戶界面self.window.rootViewController = controller;// 創(chuàng)建 UIView 對(duì)象UIView * rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];// UIViewController 顯示 rootView 控件controller.view = rootView;// 創(chuàng)建圓角按鈕UIButton * button = [UIButton buttonWithType : UIButtonTypeRoundedRect];// 設(shè)置按鈕大小button.frame = CGRectMake(60, 200, 200, 40);// 為按鈕設(shè)置文本[button setTitle:@"octopus button" forState: UIControlStateNormal];// 為按鈕添加點(diǎn)擊事件[button addTarget:self action:@selector(button_click) forControlEvents:UIControlEventTouchUpInside];// 將按鈕添加到 View 上[rootView addSubview : button] ;// 創(chuàng)建 UILabel 對(duì)象label = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 130)];// 設(shè)置文本內(nèi)容label.text = @"octopus label";// 設(shè)置背景顏色label.backgroundColor = [UIColor grayColor];// 將 UILabel 添加到 rootView 中[rootView addSubview : label];// 顯示 界面[self.window makeKeyAndVisible];return YES; }- (void) button_click {[label setText:@"button click"]; }- (void)applicationWillResignActive:(UIApplication *)application {// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. }- (void)applicationDidEnterBackground:(UIApplication *)application {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. }- (void)applicationWillEnterForeground:(UIApplication *)application {// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. }- (void)applicationDidBecomeActive:(UIApplication *)application {// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. }- (void)applicationWillTerminate:(UIApplication *)application {// Saves changes in the application's managed object context before the application terminates.[self saveContext]; }- (void)saveContext {NSError *error = nil;NSManagedObjectContext *managedObjectContext = self.managedObjectContext;if (managedObjectContext != nil) {if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {// Replace this implementation with code to handle the error appropriately.// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);abort();} } }#pragma mark - Core Data stack// Returns the managed object context for the application. // If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. - (NSManagedObjectContext *)managedObjectContext {if (_managedObjectContext != nil) {return _managedObjectContext;}NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];if (coordinator != nil) {_managedObjectContext = [[NSManagedObjectContext alloc] init];[_managedObjectContext setPersistentStoreCoordinator:coordinator];}return _managedObjectContext; }// Returns the managed object model for the application. // If the model doesn't already exist, it is created from the application's model. - (NSManagedObjectModel *)managedObjectModel {if (_managedObjectModel != nil) {return _managedObjectModel;}NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Empty" withExtension:@"momd"];_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];return _managedObjectModel; }// Returns the persistent store coordinator for the application. // If the coordinator doesn't already exist, it is created and the application's store added to it. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {if (_persistentStoreCoordinator != nil) {return _persistentStoreCoordinator;}NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Empty.sqlite"];NSError *error = nil;_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {/*Replace this implementation with code to handle the error appropriately.abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. Typical reasons for an error here include:* The persistent store is not accessible;* The schema for the persistent store is incompatible with current managed object model.Check the error message to determine what the actual problem was.If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.If you encounter schema incompatibility errors during development, you can reduce their frequency by:* Simply deleting the existing store:[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]* Performing automatic lightweight migration by passing the following dictionary as the options parameter:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.*/NSLog(@"Unresolved error %@, %@", error, [error userInfo]);abort();} return _persistentStoreCoordinator; }#pragma mark - Application's Documents directory// Returns the URL to the application's Documents directory. - (NSURL *)applicationDocumentsDirectory {return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; }@end
-- 執(zhí)行效果 : 點(diǎn)擊前;
-- 執(zhí)行后 :?
-- 工程內(nèi)容 : 該工程中只需要修改 OCAppDelegate.h 和 OCAppDelegate.m 兩個(gè)類;
2. 動(dòng)態(tài)添加刪除組件
(1) 代碼示例
動(dòng)態(tài)添加刪除組件代碼示例 :?
-- 創(chuàng)建文件 : 創(chuàng)建 Single View Application;
-- 工程文件顯示 : 只需要修改 OCViewController.m 文件即可, 其它文件原封不動(dòng);
-- OCViewController.m 代碼 :?
// // OCViewController.m // DynamicUI // // Created by octopus on 15-11-22. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved. //#import "OCViewController.h"/*類別與擴(kuò)展類別 : 為現(xiàn)有類添加新方法, @interface 已有類 (類別名) ... 方法 ... @end, 實(shí)現(xiàn)類實(shí)現(xiàn)接口中的方法.-- 頭文件名稱 : 已有類+類別名.h;-- 實(shí)現(xiàn)類文件名稱 : 已有類+類別名.m, 注意 該實(shí)現(xiàn)部分 只是實(shí)現(xiàn) 類別中定義的內(nèi)容;擴(kuò)展 : 擴(kuò)展是匿名的類別, 擴(kuò)展也要定義的 已有類+任意名.h 中, 在實(shí)現(xiàn)類 已有類.m 中需要實(shí)現(xiàn) 已有類接口中的內(nèi)容, 也需要實(shí)現(xiàn)擴(kuò)展中定義的抽象類.*/ // OCViewController 的擴(kuò)展, 在 @interface OCViewController ()@property (nonatomic, strong) NSMutableArray * labels;@end@implementation OCViewControllerint nextY = 80;- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.// 設(shè)置 View 的背景顏色self.view.backgroundColor = [UIColor grayColor];// 初始化 數(shù)組self.labels = [NSMutableArray array];// 創(chuàng)建按鈕UIButton * button_add = [UIButton buttonWithType:UIButtonTypeRoundedRect];// 設(shè)置按鈕位置大小button_add.frame = CGRectMake(30, 30, 60, 40);// 設(shè)置按鈕 平時(shí)狀態(tài)的 文本, 可以設(shè)置平時(shí)狀態(tài)時(shí)的文本[button_add setTitle:@"添加 Label" forState : UIControlStateNormal];// 設(shè)置按鈕點(diǎn)擊事件[button_add addTarget:self action:@selector(addButton) forControlEvents:UIControlEventTouchUpInside];// 將按鈕添加到布局中[self.view addSubview:button_add];// 生成刪除按鈕UIButton * button_delete = [UIButton buttonWithType:UIButtonTypeRoundedRect];// 設(shè)置按鈕的位置大小button_delete.frame = CGRectMake(230, 30, 60, 40);// 設(shè)置按鈕文本[button_delete setTitle:@"刪除 Label" forState:UIControlStateNormal];// 設(shè)置按鈕點(diǎn)擊事件[button_delete addTarget:self action:@selector(deleteButton) forControlEvents:UIControlEventTouchUpInside];// 將按鈕添加到布局中[self.view addSubview:button_delete]; }// 添加控件方法 - (void) addButton {// 生成一個(gè) UILabel 控件UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(80, nextY, 160, 30)];// 給控件設(shè)置文字label.text = @"Label 控件";// 將控件添加到集合中[self.labels addObject:label];// 將控件設(shè)置到布局中[self.view addSubview:label];// y 坐標(biāo)自增nextY += 50; }// 刪除控件方法 - (void) deleteButton {// 刪除的前提是控件個(gè)數(shù)大于 0if([self.labels count] > 0){// 將集合中的最后一個(gè) UILabel 控件從布局中刪除[[self.labels lastObject] removeFromSuperview];// 將 UILabel 控件從集合中刪除[self.labels removeLastObject];// y 坐標(biāo)自減, 用于添加控件使用nextY -= 50;} }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end
-- 演示效果 :?
3. 自定義 UI 控件
(1) 自定義組件簡介
自定義組件簡介 :?
-- UIView 基類 : 所有的 UI 控件都繼承了 UIView 基類, UI 組件在 UIView 提供的空白區(qū)域上繪制具體的細(xì)節(jié);
-- 適用情況 : 在 IOS 系統(tǒng)提供的組件不滿足需求時(shí), 就需要自定義組件;
(2) UIView 基類常用方法
UIView 基類常用方法簡介 :?
-- "initWithFrame :" 方法 : UI 控件初始化方法, 用于對(duì) UI 控件額外初始化操作時(shí)使用;
-- "initWithCoder :" 方法 : 界面設(shè)計(jì)文件中加載完該控件后, 自動(dòng)調(diào)用的方法;
-- "drawRect : " 方法 : 自行繪制控件內(nèi)容, 重寫該方法, 在該方法中實(shí)現(xiàn);
-- "layoutSubviews : " 方法 : 精確控制子控件布局;
-- "addAddSubviews : " 方法 : 添加完子控件后回調(diào)的方法;
-- "willRemoveSubview : " 方法 : 將要?jiǎng)h除子控件時(shí), 回調(diào)該方法;
-- "didMoveToSuperview : " 方法 : 把控件添加到父控件完成時(shí) 回調(diào)該方法;
-- "willMoveToSuperview : " 方法 : 在將要添加到父控件之前, 回調(diào)該方法;
-- "didMoveToWindow : " 方法 : 將控件添加到窗口完成時(shí), 回調(diào)該方法;
-- "willMoveToWindow :?" 方法 : 將控件添加到窗口之前, 回調(diào)該方法;
-- "touchesBegan : withEvent : " 方法 : 開始觸摸控件時(shí)觸發(fā)的方法;
-- "touchesMoved : withEvent : " 方法 : 手指在控件上移動(dòng)回調(diào)方法;
-- "touchesEnd : withEvent : " 方法 : 手指結(jié)束觸摸回調(diào)方法;
-- "touchesCanceled : withEvent : " 方法 : 取消觸摸回調(diào)方法;
(2) 自定義組件示例
自定義組件 流程 及 代碼示例 :?
-- 創(chuàng)建 SingleViewApplication :?
-- 查看 Main.storyboard 的控件 : 點(diǎn)擊 界面設(shè)計(jì)文件中的 UIView 根控件, 使用 command + option + 3 打開身份檢查器, 由身份檢查器上可以看到 其 Custom Class 屬性為 UIView, 因此該界面的根控件時(shí) UIView;
-- 創(chuàng)建自定義類 : 選擇菜單中的 File -> New File -> Cocoa Touch -> Objective-C Class;
-- OCFollowTouchView.h 代碼 :?
// // OCFollowTouchView.h // FollowTouch // // Created by octopus on 15-11-25. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved. //#import <Foundation/Foundation.h>@interface OCFollowTouchView : UIView@end
-- OCFollowTouchView.m 代碼示例 :?
// // OCFollowTouchView.m // FollowTouch // // Created by octopus on 15-11-25. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved. //#import "OCFollowTouchView.h"@implementation OCFollowTouchViewint currentX; int currentY;- (void) touchesMoved : (NSSet * ) touches withEvent:(UIEvent *)event {//獲取 UITouch 對(duì)象UITouch * touch = [touches anyObject];//獲取觸摸的位置CGPoint lastPoint = [touch locationInView:self];currentX = lastPoint.x;currentY = lastPoint.y;//重繪 UI 控件[self setNeedsDisplay]; }- (void) drawRect:(CGRect)rect {//獲取繪圖上下文對(duì)象CGContextRef ref = UIGraphicsGetCurrentContext();//設(shè)置所繪制的圖像為紅色CGContextSetFillColorWithColor(ref, [[UIColor redColor] CGColor]);//繪制圖像CGContextFillEllipseInRect(ref, CGRectMake(currentX - 10, currentY - 10, 20, 20)); }@end
-- 將自定義組件設(shè)置到 Main.storyboard 中 : 在 Custom Class 屬性中, 輸入自定義類, 然后點(diǎn)擊回車, 默認(rèn)的 View 就會(huì)變成我們?cè)O(shè)置的自定義類;
-- 演示效果 :?
六. UI 圖片
參考文章 :?
-- IOS 圖標(biāo)圖形 :?http://www.zhihu.com/question/20248971?;
-- IOS 7 官方推薦尺寸 : http://www.cnblogs.com/dyllove98/p/3233845.html?;
-- IOS 8 官方推薦尺寸 :?點(diǎn)擊打開鏈接?;
-- 圖標(biāo)圖像大小 : http://www.cnblogs.com/dyllove98/p/3233845.html?;
1. IOS 圖片規(guī)格
(1) Retina 視網(wǎng)膜屏
Retina 屏幕簡介 :?
-- 像素 : 高清晰度的視網(wǎng)膜屏幕, 這種屏幕的分辨率是普通非 Retina 屏幕的 2 倍;
-- 市場狀況 : 目前 IOS 已經(jīng)下架了所有的 非 Retina 屏幕的設(shè)備, 目前在售的所有設(shè)備都是 Retina 設(shè)備, 但是之前賣出的許多設(shè)備有些不是 Retina 屏幕的;
-- 支持 Retina 屏幕的設(shè)備 : 現(xiàn)在在售的所有設(shè)備, iphone4 ~ 6S, ipod4, ipad 2 之后的新設(shè)備;
-- 不支持 Retina 屏幕設(shè)備 : 之前出售的低端 IOS 設(shè)備大都是非 Retina 屏幕的, ipod 1, ipod2, ipod3, iphone, iphone3G, iphone3GS, ipad1, ipad2.
-- 素材命名規(guī)范 : 非 Retina 屏幕名稱為 pictire.png, Retina 屏幕的圖片名稱是 picture@2x.png;
(3) 圓角矩形
圓角矩形圓角 :?
-- 512 px : 90 px;
-- 114 px : 20 px;
-- 72 px : 12 px;
-- 57 px : 10 px;
-- 58 px : 10 px;
-- 29 px : 9 px;
(3) 應(yīng)用圖標(biāo)規(guī)格
Retina 屏幕應(yīng)用圖標(biāo)規(guī)格 :?
-- iPhone 4S ~ 6 (Retina @2x) : 120 x 120 ;
-- iPhone6 Plus (Retina @3x) : 180 x 180;
-- iPad 和 iPad Mini (Retina @2x) : 152 x 152 ;
非 Retina 屏幕應(yīng)用圖標(biāo)規(guī)格 :?
-- iPad 和 iPad Mini (@1x) : 76 x 76 ;
-- iPhone 和 iPod : 57 x 57;
(4) 啟動(dòng)界面像素
Retina 屏幕啟動(dòng)畫面 :?
-- iPhone 5 ~ 6 : 640 x 1136;
-- iPhone 4 ~ 4S : 640 x 960;
-- iPad : 1536 x 2048 (縱向), 2048 x 1536 (橫向);;
非 Retina 屏幕啟動(dòng)圖片 :?
-- iPad2 或 iPad Mini : 768 x 1024 (縱向), 1024 x 768 (橫向);
2. IOS 圖標(biāo)設(shè)置
IOS 圖片設(shè)置 : 直接從文件目錄將圖片拖動(dòng)到 Xcode 相應(yīng)的設(shè)置中.
-- 設(shè)置圖標(biāo) : 在項(xiàng)目文件視圖中, 點(diǎn)擊 Images.xcassets, 選中左側(cè)的 AppIcon 設(shè)置應(yīng)用圖標(biāo), 如下圖;
-- 設(shè)置啟動(dòng)圖片 :?
總結(jié)
以上是生活随笔為你收集整理的【iOS 开发】iOS 开发 简介 (IOS项目文件 | MVC 模式 | 事件响应机制 | Storyboard 控制界面 | 代码控制界面 | Retina 屏幕图片适配)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android应用开发】Android
- 下一篇: 【iOS 开发】基本 UI 控件详解 (