• Save
I os 05
Upcoming SlideShare
Loading in...5
×
 

I os 05

on

  • 289 views

 

Statistics

Views

Total Views
289
Slideshare-icon Views on SlideShare
289
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    I os 05 I os 05 Presentation Transcript

    • Class 05iOS 應⽤用軟體設計
    • 內容⼤大綱• Protocol (協定) 與 Delegate (代理) • QV094:UITextField 的代理⽅方法• UITextField Delegate • QV094:UITextField 的代理⽅方法 • QV095:UITextField ⽴立即顯⽰示輸⼊入⽂文字 • QV096:UITextField 的更多控制• UIAlertView Delegate • QV098:處理 AlertView 上其他的按鈕• UIActionSheet • QV044:UIActionSheet 由下往上彈出
    • Protocol (協定)
    • 協定的定義• 表達物件的⾏行為與⽅方法宣合的集合稱為 協定,協定與實作會怎麼改變並沒有關 係,⽽而是以對應到何種訊息的⾓角度來看 待物件,並據以系統化的⽅方法。• 善⽤用此⽅方法,便能定義出⾼高彈性與獨⽴立 性的類別。
    • Objective-C 協定• ⽅方法宣告的集合• 如何實作交給各個類別來實現• protocol 只宣告 method,也不實作• protocol 將定義 method 內容的任務交給 答應遵守 protocol 的類別
    • Objective-C 協定 (續)• 去實作某個協定的類別之間,彼此之間 有無繼承關係都無所謂• 唯⼀一必要的,是協定中被宣告的⽅方法最 後必須在代理⼈人中被實作出來
    • 協定的宣告• 語法如下: @protocol 協定名稱 - ⽅方法 (method) 的宣告; - ⽅方法 (method) 的宣告; ............ @end
    • 協定的採⽤用• 語法如下: @interface 類別名稱 : 超類別 <協定名稱> { 實體屬性的宣告; ............ } - ⽅方法的宣告; ............ 可視為協定內的 @end ⽅方法已經宣告了
    • 指定必要功能與選項功能• 例如鬧鐘的協定: @protocol Alarm - (void) setCurrentTime: (NSDate *) date; - (void) alarm; - (setAlarm: (BOOL) flag; 沒有指定的部份 @optional 等同 @required - (BOOL) snooze; - (void) setSnooze: (BOOL) flag; @required - (void) setTimerAtHour: (int)h minute:(int)m; @end
    • Project QV097protocol 練習command-line 程式
    • main.m (1/3)@protocol operation-(void) add:(int)n;-(void) sub:(int)n;-(void) mul:(int)n;-(void) div:(int)n;@end
    • @interface IntegerOpr : NSObject <operation>{ main.m (2/3) int num;}@property int num;@end (1) 指定使⽤用協定@implementation IntegerOpr@synthesize num;-(void) add:(int) n{ num += n; NSLog(@"相加後的結果為%d", num);}-(void) sub:(int)n (2) 各個⽅方法實作{ num -= n; NSLog(@"相減後的結果為%d", num);}-(void) mul:(int)n{ num *= n; NSLog(@"相乘後的結果為%d", num);}-(void) div:(int)n{ if(n!=0) num /= n; NSLog(@"相加後的結果為%d", num); else NSLog(@"不能除以零");}@end
    • int main(int argc, const char * argv[]) main.m (3/3){ @autoreleasepool { IntegerOpr *obj = [[IntegerOpr alloc] init]; [obj setNum:8]; NSLog(@"num初始值為 %d", [obj num]); NSLog(@"加2......"); 主程式使⽤用協定 [obj add:2]; NSLog(@"減3......"); [obj sub:3]; NSLog(@"乘以4......"); [obj mul:4]; 執⾏行結果 NSLog(@"除以0......"); [obj div:0]; NSLog(@"除以2......"); [obj div:2]; } return 0;}
    • Delegate(代理、妥託)
    • delegate• 代理⼈人:替當事⼈人分擔事務,協助完成• delegate 定義了許多重要的 method (即為 protocol)• 在適當的時機對應的 method 即會被呼叫
    • 想要有⼀一個『播放⾯面板』 具備功能 的元件可以直接使⽤用 希望可以藉此開發出下列應⽤用
    • iOS 的代理機制• 沒有 delegate,幾乎沒法寫程式... UIPickerView, UIScrollView, UITextView, UIWebView, UITableView, ......• 程式的⽣生命週期.....AppDelegate
    • ⼀一些註記• 協定和繼承有相似的地⽅方,但協定只有 method,沒有 member• 繼承只能單⼀一繼承 (Objective-C 沒有多重繼承)• 參考 Java 的 Interface......
    • UITextField Delegate
    • UITextField Delegate
    • UITextField Delegate (續)
    • 範例:⽂文字輸⼊入框,即時顯⽰示已輸⼊入的字數 (並進⾏行輸⼊入的檢查)QV094:指定事件的⽅方式 (xib)QV095:指定事件的⽅方式 (coding)QV096:使⽤用 Delegate 的⽅方式
    • Project QV094⽴立即顯⽰示輸⼊入⽂文字的數⺫⽬目(使⽤用 UITextField) ⽅方法之⼀一:透過 xib 指定『Editing Changed』事件注意:Value Changed 無⽤用
    • 設定⽂文字框輸⼊入鍵盤的隱藏 (2) 事件應改為 Did End On Exit(1) 預設的事件無 效,應刪除
    • ⽂文字輸⼊入之後的處理事件 (2) 事件應改為 Editing Changed(1) 事件 ValueChanged 無效
    • ViewController.h#import <UIKit/UIKit.h>@interface ViewController : UIViewController{ IBOutlet UILabel *displayLabel; IBOutlet UITextField *userInput;}-(IBAction)doneEdit:(id)sender;-(IBAction)checkInput:(id)sender;@end 使⽤用 xib 連接事件
    • ViewController.m-(IBAction)doneEdit:(id)sender{ [sender resignFirstResponder];}-(IBAction)checkInput:(id)sender{ int count = [userInput.text length]; displayLabel.text = [NSString stringWithFormat:@"%d", count];} 顯⽰示已經輸⼊入的字數
    • Project QV095⽴立即顯⽰示輸⼊入⽂文字的數⺫⽬目(使⽤用 UITextField) ⽅方法之⼆二:事件的指定寫在程式內 (addTarget)
    • ViewController.h#import <UIKit/UIKit.h>@interface ViewController : UIViewController{ IBOutlet UILabel *displayLabel; IBOutlet UITextField *userInput;}-(void)doneEdit:(id)sender;-(void)checkInput:(id)sender;@end 使⽤用程式碼連接事件 (不⽤用xib)
    • -(void)doneEdit:(id)sender ViewController.m{ [sender resignFirstResponder];}-(void)checkInput:(id)sender{ int count = [userInput.text length]; displayLabel.text = [NSString stringWithFormat:@"%d", count];}- (void)viewDidLoad 指定事件對應的程式{ [super viewDidLoad]; [userInput addTarget:self action:@selector(doneEdit:) forControlEvents:UIControlEventEditingDidEndOnExit]; [userInput addTarget:self action:@selector(checkInput:) forControlEvents:UIControlEventEditingChanged]; //[userInput addTarget:self action:@selector(checkInput:) forControlEvents:UIControlEventValueChanged]; //錯的}
    • Project QV096⽴立即顯⽰示輸⼊入⽂文字的數⺫⽬目(使⽤用 UITextField)控制輸⼊入的內容 限定⻑⾧長度 只接收(禁⽌止) 指定的字元 ⽅方法之三:使⽤用 delegate ⽅方式,實作 protocol
    • ViewController.h#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UITextFieldDelegate>{ IBOutlet UILabel *displayLabel; IBOutlet UITextField *userInput;}@end (2) 指定使⽤用協定 UITextFieldDelegate (1) 不需要指定事件⽅方法 (不需開發者⾃自訂)
    • 實作 UITextField Delegate ViewController.m (1/2)-(BOOL) textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES;}-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ int count = [textField.text length]; // 此處直接⽤用傳⼊入物件 displayLabel.text = [NSString stringWithFormat:@"%d", count]; ****** 省略部份程式 ****** return YES;}
    • ViewController.m (1/2)-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ ****** 省略部份程式 ****** // 可接受 backspace if (range.length > 0 && [string length] == 0) { return YES; ⽂文字輸⼊入過程中, } // 限定字元的⻑⾧長度 可增加多項功能 int maxLength = 6; if ([textField.text length] >= maxLength && ![string isEqualToString:@""]) { 注意在『輸⼊入完成 return NO; } 前執⾏行』 // 只接收指定的字元集 NSString *availableString = @"abcdef."; if ([availableString rangeOfString:string].location == NSNotFound) { return NO; } // 不接受指定的字元 if([string isEqualToString:@"x"]) return NO; return YES;}
    • UIAlertView
    • UIAlertViewDelegate• Instance Method......
    • Project QV098UIAlertView Delegate
    • ViewController.h#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UIAlertViewDelegate>- (IBAction) buttonClick:(id)sender;@end 指定使⽤用協定 UIAlertViewDelegate ViewController.xib
    • 簡單的⽤用法 ViewController.m (1/2)- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ UIAlertView *newAlertView; 依據按鈕的索引值判斷 switch(buttonIndex) { case 0: newAlertView = [[UIAlertView alloc] initWithTitle:@"答案是" message:@"iPhone,答對了" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; break; case 1: newAlertView = [[UIAlertView alloc] initWithTitle:@"答案是" message:@"Android,好吧" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; break; } [newAlertView show];}
    • 另⼀一種⽤用法。⽽而且內部還有另⼀一個 UIAlertView- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ UIAlertView *newAlertView; ViewController.m (2/2) if([[alertView title] isEqualToString:@"題⺫⽬目是"] ) { switch(buttonIndex) { 判斷是來⾃自哪⼀一個 UIAlertView case 0: newAlertView = [[UIAlertView alloc] initWithTitle:@"答案是" message:@"iPhone,答對了" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; break; case 1: newAlertView = [[UIAlertView alloc] initWithTitle:@"其他⼿手機" message:@"選⼀一個吧" delegate:self cancelButtonTitle:@"Android" otherButtonTitles:@"Windows", @"傳統⼿手機", @"未知", nil]; break; } } 更多的按鈕 else if ([[alertView title] isEqualToString:@"其他⼿手機"] ) { newAlertView = [[UIAlertView alloc] initWithTitle:@"你選的是" message:[NSString stringWithFormat:@"第 %d 項", buttonIndex] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; } [newAlertView show];}
    • 更多的按鈕排列是直的
    • UIActionSheet
    • UIActionSheetDelegate• Instance Methods......
    • Project QV044UIActionSheet類似UIAlerts由下往上彈出的選單
    • 程序在 ViewController.h 中增加協定<UIActionSheetDelegate> 以便處理按鈕事件準備好執⾏行 ActionSheet 的起始按鈕及事件⽅方法等,與 xib 連結- (IBAction) openActionSheet:(id)sender程式撰寫
    • ViewController.h#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UIActionSheetDelegate>{ IBOutlet UILabel *message;}- (IBAction)openActionSheet:(id)sender;- (IBAction)openAnother:(id)sender;- (IBAction)openCopyright:(id)sender; 指定使⽤用的協定@end
    • ViewController.xib
    • ViewController.m (1/2)- (IBAction)openActionSheet:(id)sender{ UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"請選擇喜愛的項⺫⽬目" delegate:self cancelButtonTitle:@"返回" destructiveButtonTitle:@"特殊紅⾊色按鈕" otherButtonTitles:@"蘋果", @"甜甜圈", @"芒果", nil]; // 也可以透過此⽅方式新增按鈕 [actionSheet addButtonWithTitle:@"增加按鈕的⽅方法"]; // 顯⽰示於畫⾯面上 [actionSheet showInView:self.view];}
    • //判斷ActionSheet按鈕事件- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 0) { message.text = @"紅⾊色特殊按鍵"; ViewController.m (2/2) } else if (buttonIndex == 1) { message.text = @"Apple喜歡你"; } else if (buttonIndex == 2) { message.text = @"Android喜歡你"; } else if (buttonIndex == 3) { message.text = @"Microsoft喜歡你"; } else { message.text = [NSString stringWithFormat: @"沒有處理 buttonIndex: %d", buttonIndex]; }}
    • //將按鈕的Title當作判斷的依據NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];if([title isEqualToString:@"蘋果"]){ message.text = @"Apple喜歡你";}else if([title isEqualToString:@"甜甜圈"]) 參考利⽤用字串做{ message.text = @"Android喜歡你"; ⽐比較的⽅方法}else if([title isEqualToString:@"芒果"]){ message.text = @"Microsoft喜歡你";}else{ message.text = @"沒有處理這個按鍵";}
    • 很多按鈕項⺫⽬目- (IBAction)openAnother:(id)sender{ UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"項⺫⽬目很多時畫⾯面還不錯" delegate:nil cancelButtonTitle:@"返回" destructiveButtonTitle:@"紅⾊色" otherButtonTitles:nil]; // 指定各個按鈕 [actionSheet addButtonWithTitle:@"AAAAAAA"]; [actionSheet addButtonWithTitle:@"BBBBBBB"]; [actionSheet addButtonWithTitle:@"CCCCCCC"]; [actionSheet addButtonWithTitle:@"DDDDDDD"]; [actionSheet addButtonWithTitle:@"EEEEEEE"]; [actionSheet addButtonWithTitle:@"FFFFFFF"]; [actionSheet addButtonWithTitle:@"GGGGGGG"]; [actionSheet addButtonWithTitle:@"HHHHHHH"]; [actionSheet addButtonWithTitle:@"XXXXXXX"]; // 顯⽰示於畫⾯面上 [actionSheet showInView:self.view];}
    • 簡單的畫⾯面- (IBAction)openCopyright:(id)sender{ NSString *aboutString = @"版權聲明 nn也可以利⽤用這種⽅方式來製作nn"; UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:aboutString delegate:nil cancelButtonTitle:@"返回" destructiveButtonTitle:nil otherButtonTitles:nil]; 指定樣式 // 顯⽰示於畫⾯面上 actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; [actionSheet showInView:self.view];}
    • 參考資料UIActionSheet的使⽤用⽅方法(http://furnacedigital.blogspot.com/2011/06/uiactionsheet.html)iPhone - UIActionSheet Example(http://eureka.ykyuen.info/2010/04/14/iphone-uiactionsheet-example/)在 iPad 上的問題 (未試做) 在iPad上不能使⽤用在ViewDidLoad, 會出現Exception錯誤, 所以拉⼀一個 Button⽤用IBAction去執⾏行 在iPad上點擊其他空⽩白地⽅方時, 會⾃自動關閉UIActionSheet, buttonIndex會回 傳3, 但是如果將cancelButtonTitle設成nil的話, buttonIndex會回傳-1
    • ......