Download presentation
Presentation is loading. Please wait.
Published by穹昭 丁 Modified 8年之前
1
明新科技大學 資訊工程系 彭亦暄 台北科技大學 資訊工程系 江佩穎 教育部資通訊軟體創新人才推升推廣計畫 iOS 智慧裝置軟體設計 iOS 常用元件
2
常用元件介紹 2 2
3
Label 提供一個顯示的文字的 view 3 3
4
Label Attributes 4 4 Text :設定要顯示的文字 Color : 文字的顏色 Font :設定文字字型與大小 Baseline :設定文字基線的對齊方式 - Align Baseline :對齊基線 - Align Centers :對齊 View 的中央 - Align None :對齊 View 的左上方 Line Breaks :字串超出 View 寬度的處理方式 - Clip :直接將超出範圍的字串裁掉 - Character Wrap :以字元為單位做換行 - Word Warp :以單字為單位做換行 - Truncate Head :不顯示句子開頭的文字 - Truncate Middle : 不顯示句子中間的文字 - Truncate Tail :不顯示句子尾端的文字 Shadow :設定陰影
5
View Attributes 5 5 Tag :標記,可用來辨識不同的元件 Interaction :設定元件是否可以與使用者互動 Multiple Touch: 是否支援多點觸控 Alpha :設定元件的透明度 Background :設定背景顏色 Drawing : Opaque :不透明 Hidden :設定隱藏元件 Clear Graphics Context : 將元件放置區域, 是否要先清除。 Clip Subviews :若勾選,超出 superview 的元 件部分會被切掉。 Autoresize Subviews :自動調整 subview 大小
6
Button 按鈕可以監聽多種觸碰的事 件 當觸碰事件被觸發時,會傳 送動作訊息到目標的物件 6 6
7
Button Attributes 7 7 Type: 選擇按鈕的類別 - Custom - System - Detail Disclosure - Info Light - Info Dark - Add Contact State Config: 狀態設定 - Default - Highlighted - Selected - Disabled Title: 設定按鈕上的文字 Text Color: 設定文字顏色 Shadow Color: 設定陰影顏色 Image: 用自訂的圖片作為按鈕 Background: 設定背景樣式
8
Button Attributes 8 8 Drawing: Reverses On Highlight : 當按鈕被 Highlight 時 按鈕標題字陰影是否要改變 Shows Touch On Highlight: 當按鈕被按下時 按鈕是否要標示起來 Highlighted Adjusts Image: 當按鈕被 Highlight 時按鈕 Image 是否要改變 Disabled Adjusts Image: 當按鈕被 disabled 時 按鈕 Image 是否要改變 Edge / Inset Content: 包含按鈕內全部內容的矩形區域的 邊界 Title: 包含按鈕標題內容的矩形區域的邊界 Image: 包含按鈕圖示內容的矩形區域的邊界
9
Control Attributes 9 9
10
TextField 提供一個可編輯的文字區塊 10
11
TextField 11 Text: 設定顯示的文字 Color: 設定文字顏色 Font: 設定文字字型與大小 Alignment: 設定對齊的方式 Placeholder: 設定提示的文字 Background: 設定背景圖片 Disabled: 設定元件 Disabled 時的背景圖片 Border Style: 設定邊框的樣式 Clear Button: 設定文字欄位清除按鈕 -Never appears -Appears while editing -Appears unless editing -Is always visible Capitalization :設定英文大寫 -None :不需要全部大寫 -Words :每個單字的第一個字母大寫 -Sentences :每段句子的第一個字母大寫 -All Characters :全部的字母都是大寫
12
TextField 12 Correction :字串修正設定 -Default :預設 -No :不需要修正 -Yes :需要修正 Keyboard: 設定鍵盤的格式 Appearance: 設定鍵盤出現的樣式 Return Key: 設定返回鍵的功能 Auto-enable Return Key :僅有輸入內容時才有 Return Key 的功能。 Secure: 輸入的文字會隱藏,改以點表示。
13
鍵盤樣式 -1 - Default , ASCII Capable - Number &Punctuation - URL - Number Pad 13
14
鍵盤樣式 -2 - Phone Pad - Name Phone Pad - Email Address 14
15
鍵盤樣式 -3 ihsuan.peng@gmail.com 15
16
自訂 UIViewController 16 iOS SDK 中,大部分 UI 元件的類別皆是以 UI 開頭。 IBOutlet /IBAction: Interface Builder 並不會知道所有在 Xcode 裡定義的詳細資訊,只有前面加 IBOutlet 和 IBAction , 才能知道 @interface XXX ViewController : UIViewController @end XXXViewController.h
17
建立 IBOutlet 關聯 打開 assistant 視窗,選取元件,按下 Ctrl 按鍵,依下圖用滑鼠連接 17
18
建立 IBOutlet 關聯 18 輸入物件變數名稱和選取屬性 依序將兩個 TextField 和下面的 Label 建立連接關係
19
建立 IBAction 關聯 19 與建立 IBOutlet 關聯相同,選取元件,按下 Ctrl 按 鍵,建立連接線到.h 檔中 輸入方法名稱 和選取事件類型
20
Button 相關事件介紹 (1/2) Touch Cancel :觸碰按鈕時,因為有來電等原因 導致應用程式中止動作時,所觸發的事件 Touch Down :觸碰按鈕的瞬間所觸發的事件。 Touch Down Repeat :連續觸碰按鈕兩次所觸發 的事件 Touch Drag Enter :從按鈕外拖曳回按鈕內時, 所觸發的事件。 Touch Drag Exit :觸碰按鈕後,拖曳出按鈕外時, 所觸發的事件。 20
21
Button 相關事件介紹 (2/2) Touch Drag Inside :觸碰按鈕後,手指拖曳於按 鈕內時,將連續觸發此事件。 Touch Drag Outside :觸碰按鈕後,手指拖曳於 按鈕外面時,將連續觸發此事件。 Touch Up Inside :觸碰按鈕或於按鈕內拖曳,直 到手指於按鈕內離開才觸發此事件。 Touch Up Outside :手指於按鈕外離開時才觸發 此事件。 21
22
編寫標頭檔 (.h) 22 #import @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *heightField; @property (weak, nonatomic) IBOutlet UITextField *weightField; @property (weak, nonatomic) IBOutlet UILabel *showField; - (IBAction)btnClick:(id)sender; @end XXXViewController.h
23
編寫實作檔 (.m) 23 XXXViewController.m #import "ViewController.h” @implementation ViewController - (void)viewDidLoad{ [super viewDidLoad]; } - (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; } - (IBAction)btnClick:(id)sender { double height=0,weight=0,result=0; if(self.heightField.text.length>0 && self.weightField.text.length>0){ height=self.heightField.text.doubleValue/100; weight=self.weightField.text.doubleValue; } if (height>0) result=weight/(height*height); self.showField.text=[NSString stringWithFormat:@"%f",result]; } @end
24
UIViewController 的 viewDidLoad iPhone 程式啟動後,會讀取 xib 檔,產生對應 UI 元件後, viewDidLoad 會被呼叫,此時可對元件進行修改,畫面還 未呈現 -(void)viewDidLoad { [self.myButton setTitle:@"Hello" forState:UIControlStateNormal]; [self.myButton setTitle:@"Hi" forState:UIControlStateHighlighted]; [super viewDidLoad]; } 24
25
多連結 一個 IBOutlet 只能對應一個元件 一個 IBAction 可對應各元件 如何區別 ? (IBAction)displayButton: (id) sender; sender 為觸發事件的元件,可由 tag 屬性作區別 @property(nonatomic) NSInteger tag 25
26
Views 26
27
View 相關類別 27 NSObject UIResponder UIViewUIWindow UILabel UIControl UIAlertView UIScrollView UITableView UITextView UIButton UITextField ………… ………… UIViewController UIApplication
28
UIResponder 定義物件回應和處理事件的介面 touch events motion events Managing the Responder -(BOOL)becomeFirstResponder -(BOOL)resignFirstResponder … 28
29
鍵盤消失 輸入文字時,按 return 時,把鍵盤消失 將 IBAction 方法連接到元件的 Did End on Exit -(IBAction)done:(id)sender { [sender resignFirstResponder]; } 29
30
按下按鈕鍵盤消失 30 - (IBAction)btnClick:(id)sender { ….. [self.heightField resignFirstResponder]; [self.weightField resignFirstResponder]; }
31
UIViewController 提供基本的 view 管理 管理 Views - (void)viewDidLoad 當 view controller 載入 view(outlets 也被設定好 ) 到記憶體後會呼 叫此方法 回應 view 事件 - (void)viewWillAppear:(BOOL)animated - (void)viewWillLayoutSubviews - (void)viewDidLayoutSubviews - (void)viewDidAppear:(BOOL)animated - (void)viewWillDisappear:(BOOL)animated - (void)viewDidDisappear:(BOOL)animated 31
32
UIViewController 提供基本的 view 管理 設定 View 旋轉 (BOOL)shouldAutorotate 回傳 view controller’s 的內容是否自動跟著裝置 旋轉 (NSUInteger)supportedInterfaceOrientations 回傳 view controller 支援的方向 UIInterfaceOrientationMask 32
33
UIInterfaceOrientationMask 33 typedef enum { UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait), UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft), UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight), UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown), UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown), UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), } UIInterfaceOrientationMask;
34
Views view (i.e. UIView subclass) 表示一個矩形空間 定義坐標空間 負責矩形區域的繪製和事件處理 階層架構 只有一個 superview - (UIView *)superview 可以有多個 ( 或零 ) subviews - (NSArray *)subviews Subview 順序 (in that array): 越後加入的在最頂層,越靠近使 用者 34
35
Views 階層架構通常由 Interface Builder 建立,也可以使用程式 建立 (void)addSubview:(UIView *)aView; (void)removeFromSuperview; 管理 subviews 順序 (void)insertSubview:(UIView *)aView atIndex:(int)index; (void)insertSubview:(UIView *)aView belowSubview:(UIView *)otherView; (void)insertSubview:(UIView *)aView aboveSubview:(UIView *)otherView; 35
36
View 隱藏屬性 @property BOOL hidden; myView.hidden = YES; // view 不會在螢幕上且不會處理事件 36
37
使用 Objective-C code 設計 UI 37
38
Coordinates CGFloat 浮點數,在圖形中經常被使用 CGPoint 包含兩個 CGFloat(x,y) 的 C 結構 CGPoint p = CGPointMake(34.5, 22.0); p.x += 20; // 往右移動 20 點 38 typedef float CGFloat; struct CGPoint { CGFloat x; CGFloat y; }; typedef struct CGPoint CGPoint;
39
Coordinates CGSize 包含兩個 CGFloat(width, height) 的 C 結構 CGSize s = CGSizeMake(100.0, 200.0); s.height += 50; // 高度加 50 點 CGRect 包含 1 個 CGPoint(origin ) 和 1 個 CGSize(size) 的 C 結構. CGRect aRect = CGRectMake(45.0, 75.5, 300, 500); aRect.size.height += 45; aRect.origin.x += 30; 39 struct CGSize { CGFloat width; CGFloat height; }; typedef struct CGSize CGSize; struct CGRect { CGPoint origin; CGSize size; }; typedef struct CGRect CGRect;
40
Views 與位置和大小相關的屬性 @property CGRect bounds; View 內部空間的座標和大小 @property CGPoint center; View 的中心點座標 ( 在 superview’s 座標空間 ) @property CGRect frame; a rectangle in your superview’s coordinate space which entirely contains your view’s bounds.size 40
41
Coordinates 41 View B’s bounds = ((0,0),(200,250)) View B’s frame = ((140,65),(320,320)) View B’s center = (300,225) View B View A (0,0) 200 250 (140,65) 320 (300,225)
42
建立 Views 如何用程式建立 UIView? 使用 alloc 和 initWithFrame: (UIView’s designated initializer). Example CGRect buttonRect = CGRectMake(20, 20, 120, 37); UIButton *button = [[UIButton alloc] initWithFrame:buttonRect]; button.backgroundColor=[UIColor redColor]; [self.view addSubview:button]; 42
43
UIControl 當事件發生時,定義準備動作訊息和事件配發到 目的地的介面和基本實作 方法包含 取得和設定控制的狀態 tracking touches within a control 43
44
UIControl - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents target: the object to which the action message is sent. action: A selector identifying an action message controlEvents: specifying the control events for which the action message is sent 44
45
UIControl - (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event Response to a given event, forwards an action message to the application object for dispatching to a target. 45
46
UIControl Control Events 46 enum { UIControlEventTouchDown = 1 << 0, UIControlEventTouchDownRepeat = 1 << 1, UIControlEventTouchDragInside = 1 << 2, UIControlEventTouchDragOutside = 1 << 3, UIControlEventTouchDragEnter = 1 << 4, UIControlEventTouchDragExit = 1 << 5, UIControlEventTouchUpInside = 1 << 6, UIControlEventTouchUpOutside = 1 << 7, UIControlEventTouchCancel = 1 << 8, UIControlEventValueChanged = 1 << 12, UIControlEventEditingDidBegin = 1 << 16, UIControlEventEditingChanged = 1 << 17, UIControlEventEditingDidEnd = 1 << 18, UIControlEventEditingDidEndOnExit = 1 << 19, UIControlEventAllTouchEvents = 0x00000FFF, UIControlEventAllEditingEvents = 0x000F0000, UIControlEventApplicationReserved = 0x0F000000, UIControlEventSystemReserved = 0xF0000000, UIControlEventAllEvents = 0xFFFFFFFF };
47
練習 用 code 方式, 在 view 上加入一個 UIButton 加入一個 action ,處理按鈕 UIControlEventTouchUpInside 事件 47 CGRect rect=CGRectMake(0, 0, 70, 70); UIButton * myButton=[[UIButton alloc] initWithFrame:rect]; [myButton setTitle:@"Press" forState:UIControlStateNormal]; [myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [myButton setBackgroundColor:[UIColor whiteColor]]; [myButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:myButton];
48
基本元件與機制 48
49
Application Life-Cycle 49 User taps icon on home screen main() UIApplicationMain() Event Loop System requests termination Application actually terminates
50
50
51
main.m main.m C 語言的入口點 int main(int argc, char *argv[]) 建立一個捕捉 autorelease 的 pool 呼叫 UIApplicationMain 以建立一個 UIApplication 物件 並啟動一個 run loop 51
52
main.m 應用程式入口 52 #import #import “AppDelegate.h“ int main(int argc, char *argv[]){ @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } 產生自動釋放記憶體的 pool 回傳類別名稱字串 回傳類別物件
53
UIApplicationMain int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); 從參數 principalClassName 建立一個 UIApplication 物 件 從參數 delegateClassName 建立一個 application delegate 物件並設定為此應用程式的 delegate 建立一個 main event loop 包含應用程式的 run loop 和 開始處理事件程序 53
54
UIApplicationMain int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); principalClassName 要被建立的 UIApplication 類別或子類別名稱 若設定 nil: UIApplication 會被建立 delegateClassName: 要被建立的 application delegate 類別名稱 若設定 nil: 會從應用程式的主要 nib 檔 ( 定義在 plist 中 ) 載入 delegate 物件 54
55
Application Delegate 協助 UIApplication 做事件處理 監測應用程式 high level 或 critical 事件 Launch Terminate Memory warnings Conforms to Objective-C protocol (all methods optional) 55
56
Object-C 的 protocol 類似 Java 程式中的介面 只有方法,沒有成員 方法只有宣告,沒有內容 56 使用者觸控或輸入 介面變更狀態 ( 如按下 ) 介面變更為完成狀態 API 所提供部分 觸控或輸入需客制化 的程序 ( 如進行加減運 算 )
57
Object-C 的 protocol 實作 protocol 的方法 在類別名稱後,用 標記,裡面放 protocol 的名稱 一個類別可同時實作多個 protocol ,用逗點分開 protocol 中利用 @required 跟 @optional 標示此 protocol 在實作時,方法是否必需一定要實作 預設未標明則為 @required 57 (xxx.h) @interface AppDelegate : UIResponder @end
58
UIApplicationDelegate 58 @protocol UIApplicationDelegate @optional -(void)applicationDidFinishLaunching:(UIApplication *) application; -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions ; -(void)applicationDidBecomeActive:(UIApplication *)application; -(void)applicationWillResignActive:(UIApplication *)application; … @end
59
Application delegate 59 #import @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @end protocol pointing to the top-level view for this application (UIWindow)
60
Application delegate 程式準備好可以執行時,此方法會被呼叫 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ return YES; } 60
61
61 User touches icon on home screen System calls main() main() calls UIApplicationMain() UIApplicationMain() creates instance of UIApplication UIApplication instance loads xib file, sets up based on application properties UIApplication instance goes into run loop, waiting for and forwarding events to interface elements (instances of UIResponder) User does termination UIApplication instance tells delegate that the application is terminating UIApplicationMain() exits, main() exits, process exits
62
UIApplication 應用程式的集中控制點,負責與 iOS 進行應用程 式控制和協調 處理事件初始的繞路 應用程式物件通常會指定一個 delegate application informs of significant runtime events (application launch, low-memory warnings, and application termination) giving it an opportunity to respond appropriately. 62
63
UIApplication + (UIApplication *)sharedApplication 回傳應用程式物件 設定和取得 Delegate Property id delegate 63
64
UIApplication 控制應用程式外觀 - (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation - (void)setStatusBarStyle:(UIStatusBarStyle)statusBarSt yle animated:(BOOL)animated 64 typedef enum { UIStatusBarAnimationNone, UIStatusBarAnimationFade, UIStatusBarAnimationSlide, } UIStatusBarAnimation; typedef enum { UIStatusBarStyleDefault, UIStatusBarStyleBlackTranslucent, UIStatusBarStyleBlackOpaque } UIStatusBarStyle;
65
UIApplication 控制應用程式外觀 Property BOOL statusBarHidden UIStatusBarStyle statusBarStyle CGRect statusBarFrame 65
66
UIApplication Property UIApplicationState applicationState 66 typedef enum { UIApplicationStateActive, UIApplicationStateInactive, UIApplicationStateBackground } UIApplicationState;
67
UIApplication Notifications UIApplicationDidFinishLaunchingNotification UIApplicationWillResignActiveNotification UIApplicationDidEnterBackgroundNotification UIApplicationWillEnterForegroundNotification UIApplicationDidBecomeActiveNotification UIApplicationWillTerminateNotification UIApplicationDidReceiveMemoryWarningNotification UIApplicationSignificantTimeChangeNotification UIApplicationDidChangeStatusBarOrientationNotification UIApplicationDidChangeStatusBarFrameNotification 67
68
UIApplicationDelegate UIApplication 會將接收到的事件 ( 應用程式啟動 與終止、記憶體不足警告、狀態列方向改變等 ) 轉交給代理程式 68
69
UIApplicationDelegate 應用程式生命週期相關事件 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 初始化應用程式並準備執行 應用程式被啟動且 nib 檔案已經載入後,此方法會被呼叫 -(void)applicationDidBecomeActive:(UIApplication *)application 重新啟動當應用程式 inactive 時被停止的任務 -(void)applicationWillResignActive:(UIApplication *)application 暫停執行的任務,停止 timers… 69
70
UIApplicationDelegate 應用程式生命週期相關事件 -(void)applicationDidEnterBackground:(UIApplication *) application 釋放共享資源, 儲存使用者資料, 解除 timers, 儲存足夠的應用程式 狀態資訊,以便回復時能恢復原狀態 -(void)applicationWillEnterForeground:(UIApplication *) application -(void)applicationWillTerminate:(UIApplication *) application 70
71
71 User taps icon on home screen main() UIApplicationMain() Event Loop System requests termination Application actually terminates application: didFinishLaunchingWithOptions: applicationWillTerminate: 處理事件 applicationDidBecomeActive:
72
應用程式移至背景,再回到前 景 72 applicationWillResignActive: applicationDidBecomeActive: applicationDidEnterBackground: applicationWillEnterForeground: Press Home Button 切換回原應用 程式
73
UIApplicationDelegate 管理 Status Bar 改變 (void)application:(UIApplication *)application willChangeStatusBarOrientation: (UIInterfaceOrientation)newStatusBarOrientation duration: (NSTimeInterval)duration (void)application:(UIApplication *)application didChangeStatusBarOrientation: (UIInterfaceOrientation)oldStatusBarOrientation (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFram e (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame 73
74
UIApplicationDelegate Responding to System Notifications (void)applicationDidReceiveMemoryWarning:(UIApplic ation *) application (void)applicationSignificantTimeChange:(UIApplication *) application 74
75
觀察 Application 生命週期 在 UIAppliactionDelegate 中觀察應用程式生命週 期相關事件 加入 NSLog ,觀察 log 觀察 程式啟動 按 Home 鍵進入背景 程式從背景到前景 螢幕上鎖 螢幕解鎖 75
76
程式啟動 didFinishLaunchingWithOptions viewDidLoad viewWillAppear viewDidLayoutSubviews applicationDidBecomeActive viewDidAppear 76
77
按 Home 進入背景 applicationWillResignActive applicationDidEnterBackground 77
78
切回前景 applicationWillEnterForeground applicationDidBecomeActive 78
79
結束程式 viewDidDisappear applicationWillTerminate 79
80
程式啟動流程 80 main() UIApplicationMain() didFinishLaunchingWithOptions Event Loop 程式啟動 viewDidLoad viewWillAppear viewDidLayoutSubviews applicationDidBecomeActive viewDidAppear applicationWillResignActive 按 Home 進入背景 applicationDidEnterBackground applicationWillEnterForeground 切回前景 applicationDidBecomeActive viewDidDisappear 結束程式 applicationWillTerminate UIApplicationDelegate UIViewController didFinishLaunchingWithOptions viewWillLayoutSubviews viewWillDisappear
81
應用程式相關類別 81
82
UIDevice 提供設備相關資訊 + (UIDevice *)currentDevice Returns an object representing the current device Property Identifying the Device and Operating System NSString * name NSString * systemName NSString * systemVersion NSString * Model @”iPhone”, @”iPod touch” UIUserInterfaceIdiom userInterfaceIdiom 82 typedef enum { UIUserInterfaceIdiomPhone, UIUserInterfaceIdiomPad, } UIUserInterfaceIdiom;
83
UIDevice Property Getting the Device Orientation UIDeviceOrientation orientation 83 typedef enum { UIDeviceOrientationUnknown, UIDeviceOrientationPortrait, UIDeviceOrientationPortraitUpsideDown, UIDeviceOrientationLandscapeLeft, UIDeviceOrientationLandscapeRight, UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown } UIDeviceOrientation;
84
UIDevice Property Getting the Device Battery State float batteryLevel from 0.0 (fully discharged) to 1.0 (100% charged). If battery monitoring is not enabled, battery state is UIDeviceBatteryStateUnknown and the value of this property is - 1.0. BOOL batteryMonitoringEnabled Enable/disable battery monitoring, default value: NO UIDeviceBatteryState batteryState 84 typedef enum { UIDeviceBatteryStateUnknown, UIDeviceBatteryStateUnplugged, UIDeviceBatteryStateCharging, UIDeviceBatteryStateFull, } UIDeviceBatteryState;
85
UIDevice Property Using the Proximity Sensor BOOL proximityMonitoringEnabled Enable / disable proximity monitoring. The default value is NO. BOOL proximityState A Boolean value indicating whether the proximity sensor is close to the user (YES) or not (NO). 85
86
UIDevice 產生裝置方向相關的 Notifications 方法 - (void)beginGeneratingDeviceOrientationNotification s -Begins the generation of notifications of device orientation changes. - (void)endGeneratingDeviceOrientationNotifications -Ends the generation of notifications of device orientation changes 86
87
UIDevice Notifications Notifications UIDeviceBatteryLevelDidChangeNotification Posted when the battery level changes. UIDeviceBatteryStateDidChangeNotification Posted when battery state changes UIDeviceOrientationDidChangeNotification Posted when the orientation of the device changes UIDeviceProximityStateDidChangeNotification Posted when the state of the proximity sensor changes. 87
88
Notification 採用廣播方式,將發送出去的訊息廣播給註冊過 的觀測者,觀測者對訊息進行處理 NSNotificationCenter 訊息中心,每一個 Process 都會有一個此物件 + (id)defaultCenter Returns the process’s default notification center. 88 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
89
Notification Managing Notification Observers - (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender notificationObserver: 要註冊接收 notification 的物件 ( 觀測者 ) notificationSelector notification 發生時,要傳遞至觀測者的訊息 此參數為一個方法,此方法必須要有一個 NSNotification 的參數 notificationName notifications 的名稱 notificationSender Notification 的發送者 89
90
Notification 管理 Notification 觀測者 - (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender notificationObserver: 要移除的觀測者 notificationName : 要移除的 notifications 的名稱 notificationSender: 要移除的發送者 90
91
手機方向改變訊息通知 91 NSNotificationCenter *center= [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(observ:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]]; -(void)observ:(NSNotification *)notify{ UIDevice *d=[notify object]; NSLog(@"%d ",[d orientation]); } [center removeObserver:self name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
92
Notification 產生 Notifications - (void)postNotification:(NSNotification *)notification - (void)postNotificationName:(NSString *) notificationName object:(id)notificationSender - (void)postNotificationName:(NSString *) notificationName object:(id)notificationSender userInfo:(NSDictionary *) userInfo notificationName: Notification 的名稱 notificationSender: notification 的發送者 userInfo: Notification 載送的訊息 92
93
發送訊息 93 NSNotificationCenter *center= [NSNotificationCenter defaultCenter]; NSDictionary *info=[[NSDictionary alloc] initWithObjectsAndKeys : @"1st Value", @"1", @"2st Value", @"2", nil]; [center addObserver :self selector:@selector(observ:) name:@"CHANGED" object : self ]; [center postNotificationName:@"CHANGED" object: self userInfo: info]; -(void)observ:(NSNotification *) notify{ NSDictionary *info=[notify userInfo]; for (id x in info) { NSLog(@"key=%@ value=%@ ",x,[info objectForKey:x]); }
94
練習 偵測 Memory Warning Notification 在 NSLog 中顯示記憶體警告 94
95
UIAlertView 95
96
UIAlertView 4 個部分組成 title message cancelButtonTitle 若 Button 兩個,左右排列,此設定左邊按鈕文字 若 Button 數量 >2 ,上到下排列,此設定最下面按鈕文字 otherButtonTitles 96
97
UIAlertView 建立 Alert Views -(id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles,... 顯示 (void)show 消失 (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated 97
98
(void)viewDidLoad 98 UIAlertView *myalert=[[UIAlertView alloc] initWithTitle:@" 標題 " message:@" 訊息 " delegate:nil cancelButtonTitle:@" 取消 " otherButtonTitles:@"YES", @"NO", nil]; [myalert show]; 120120
99
選擇事件 類別需實作 UIAlertViewDelegate 當按鈕被按下時,會執行以下方法 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex alertView:The alert view containing the button buttonIndex:The position of the clicked button. The button indices start at 0. 99
100
練習 畫面中有一個 UIButton 和一個 UILabel 點選 UIButton ,產生三個選項的 AlertView ,點 選其中一個按鈕後,將選取按鈕的索引值顯示到 Label 上 100
101
UIAlertView iOS5 新增 屬性 UIAlertViewStyle alertViewStyle typedef enum { UIAlertViewStyleDefault = 0, UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, UIAlertViewStyleLoginAndPasswordInput } UIAlertViewStyle; (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex 取得索引值位置的 TextField 元件 101 索引值 0 login 0 pwd 1
102
102 - (void)viewDidLoad{ [super viewDidLoad]; UIAlertView *myalert=[[UIAlertView alloc] initWithTitle:@" 請輸入帳號密碼 " message:nil delegate:self cancelButtonTitle:@" 取消 " otherButtonTitles:@" 確定 ",nil]; [myalert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; [myalert show]; } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex==1) { UITextField * login=[alertView textFieldAtIndex:0]; UITextField * pwd=[alertView textFieldAtIndex:1]; NSLog(@"%@,%@",login.text,pwd.text); }
Similar presentations