SlideShare uma empresa Scribd logo
1 de 56
Class 05
iOS 應⽤用軟體設計
內容⼤大綱
•   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 QV097


protocol 練習
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) 事件 Value
Changed 無效
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 QV098



UIAlertView 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 QV044


UIActionSheet
類似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
......

Mais conteúdo relacionado

Mais procurados

Wind.js无障碍调试与排错
Wind.js无障碍调试与排错Wind.js无障碍调试与排错
Wind.js无障碍调试与排错
jeffz
 
Javascript之昨是今非
Javascript之昨是今非Javascript之昨是今非
Javascript之昨是今非
Tony Deng
 
Script with engine
Script with engineScript with engine
Script with engine
Webrebuild
 
Js的国(转载)
Js的国(转载)Js的国(转载)
Js的国(转载)
Leo Hui
 
Ghost cat 以皮肤为主体的ui框架(唐翎)
Ghost cat 以皮肤为主体的ui框架(唐翎)Ghost cat 以皮肤为主体的ui框架(唐翎)
Ghost cat 以皮肤为主体的ui框架(唐翎)
FLASH开发者交流会
 
JavaScript现代化排错实践
JavaScript现代化排错实践JavaScript现代化排错实践
JavaScript现代化排错实践
jeffz
 
所谓闭包
所谓闭包所谓闭包
所谓闭包
ilovey4
 
Ejb工作原理学习笔记
Ejb工作原理学习笔记Ejb工作原理学习笔记
Ejb工作原理学习笔记
yiditushe
 

Mais procurados (19)

C++11 smart pointers
C++11 smart pointersC++11 smart pointers
C++11 smart pointers
 
Ken20150320
Ken20150320Ken20150320
Ken20150320
 
I os 14
I os 14I os 14
I os 14
 
JS2
JS2JS2
JS2
 
I os 08
I os 08I os 08
I os 08
 
Wind.js无障碍调试与排错
Wind.js无障碍调试与排错Wind.js无障碍调试与排错
Wind.js无障碍调试与排错
 
Php & Mysql
Php & MysqlPhp & Mysql
Php & Mysql
 
Javascript之昨是今非
Javascript之昨是今非Javascript之昨是今非
Javascript之昨是今非
 
Python元組,字典,集合
Python元組,字典,集合Python元組,字典,集合
Python元組,字典,集合
 
JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1
 
JavaScript Advanced Skill
JavaScript Advanced SkillJavaScript Advanced Skill
JavaScript Advanced Skill
 
Script with engine
Script with engineScript with engine
Script with engine
 
107个常用javascript语句 oss 计算技术 - ossez info of tech
107个常用javascript语句   oss 计算技术 - ossez info of tech107个常用javascript语句   oss 计算技术 - ossez info of tech
107个常用javascript语句 oss 计算技术 - ossez info of tech
 
Js的国(转载)
Js的国(转载)Js的国(转载)
Js的国(转载)
 
Ghost cat 以皮肤为主体的ui框架(唐翎)
Ghost cat 以皮肤为主体的ui框架(唐翎)Ghost cat 以皮肤为主体的ui框架(唐翎)
Ghost cat 以皮肤为主体的ui框架(唐翎)
 
JavaScript现代化排错实践
JavaScript现代化排错实践JavaScript现代化排错实践
JavaScript现代化排错实践
 
所谓闭包
所谓闭包所谓闭包
所谓闭包
 
第4章函数
第4章函数第4章函数
第4章函数
 
Ejb工作原理学习笔记
Ejb工作原理学习笔记Ejb工作原理学习笔记
Ejb工作原理学习笔记
 

Destaque (11)

I os 03
I os 03I os 03
I os 03
 
I os 15
I os 15I os 15
I os 15
 
課程規畫
課程規畫課程規畫
課程規畫
 
I os 09
I os 09I os 09
I os 09
 
I os 11
I os 11I os 11
I os 11
 
I os 16
I os 16I os 16
I os 16
 
I os 07
I os 07I os 07
I os 07
 
I os 06
I os 06I os 06
I os 06
 
I os 04
I os 04I os 04
I os 04
 
I os 10
I os 10I os 10
I os 10
 
I os 13
I os 13I os 13
I os 13
 

Semelhante a I os 05

透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps
Eric ShangKuan
 
09 UITableView and UITableViewController
09 UITableView and UITableViewController09 UITableView and UITableViewController
09 UITableView and UITableViewController
Tom Fan
 
由一个简单的程序谈起--之四
由一个简单的程序谈起--之四由一个简单的程序谈起--之四
由一个简单的程序谈起--之四
yiditushe
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文
Guo Albert
 
4.2第四章 swarm代码剖析 可选补充课程
4.2第四章 swarm代码剖析 可选补充课程4.2第四章 swarm代码剖析 可选补充课程
4.2第四章 swarm代码剖析 可选补充课程
zhang shuren
 

Semelhante a I os 05 (20)

I os 01
I os 01I os 01
I os 01
 
Android 智慧型手機程式設計
Android 智慧型手機程式設計Android 智慧型手機程式設計
Android 智慧型手機程式設計
 
Ios
IosIos
Ios
 
11 UINavigationController
11 UINavigationController11 UINavigationController
11 UINavigationController
 
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAPiOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
iOS App 開發 -- Storybard 基礎練習、APP 上架、IAP
 
透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps
 
15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCell
 
09 UITableView and UITableViewController
09 UITableView and UITableViewController09 UITableView and UITableViewController
09 UITableView and UITableViewController
 
由一个简单的程序谈起--之四
由一个简单的程序谈起--之四由一个简单的程序谈起--之四
由一个简单的程序谈起--之四
 
利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geek利用Signalr打造即時通訊@Tech day geek
利用Signalr打造即時通訊@Tech day geek
 
Actionscript遊戲元素
Actionscript遊戲元素Actionscript遊戲元素
Actionscript遊戲元素
 
07 View Controllers
07 View Controllers07 View Controllers
07 View Controllers
 
2016輕鬆開發自有網路地圖工作坊 進階班 0701
2016輕鬆開發自有網路地圖工作坊 進階班 07012016輕鬆開發自有網路地圖工作坊 進階班 0701
2016輕鬆開發自有網路地圖工作坊 進階班 0701
 
沈阳师范大学课程实践 Java语言部分
沈阳师范大学课程实践 Java语言部分沈阳师范大学课程实践 Java语言部分
沈阳师范大学课程实践 Java语言部分
 
Reactive X 响应式编程
Reactive X 响应式编程Reactive X 响应式编程
Reactive X 响应式编程
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文
 
用 Standalone Component 來寫 Angular 吧! - STUDY4.TW 2023 小聚 - 聊前端
用 Standalone Component 來寫 Angular 吧! - STUDY4.TW 2023 小聚 - 聊前端 用 Standalone Component 來寫 Angular 吧! - STUDY4.TW 2023 小聚 - 聊前端
用 Standalone Component 來寫 Angular 吧! - STUDY4.TW 2023 小聚 - 聊前端
 
4.2第四章 swarm代码剖析 可选补充课程
4.2第四章 swarm代码剖析 可选补充课程4.2第四章 swarm代码剖析 可选补充课程
4.2第四章 swarm代码剖析 可选补充课程
 
UIKit-Swift
UIKit-SwiftUIKit-Swift
UIKit-Swift
 
2023-netconf-deploy-azure-function-with-KEDA-on-aks
2023-netconf-deploy-azure-function-with-KEDA-on-aks2023-netconf-deploy-azure-function-with-KEDA-on-aks
2023-netconf-deploy-azure-function-with-KEDA-on-aks
 

Mais de 信嘉 陳 (12)

Processing 06
Processing 06Processing 06
Processing 06
 
Processing 05
Processing 05Processing 05
Processing 05
 
Processing 04
Processing 04Processing 04
Processing 04
 
Processing 03
Processing 03Processing 03
Processing 03
 
Processing 02
Processing 02Processing 02
Processing 02
 
Processing 01
Processing 01Processing 01
Processing 01
 
Processing 09
Processing 09Processing 09
Processing 09
 
Processing 08
Processing 08Processing 08
Processing 08
 
Processing 07
Processing 07Processing 07
Processing 07
 
Google 街景
Google 街景Google 街景
Google 街景
 
社群網站 Facebook
社群網站 Facebook社群網站 Facebook
社群網站 Facebook
 
網路搜尋
網路搜尋網路搜尋
網路搜尋
 

I os 05

  • 2. 內容⼤大綱 • Protocol (協定) 與 Delegate (代理) • QV094:UITextField 的代理⽅方法 • UITextField Delegate • QV094:UITextField 的代理⽅方法 • QV095:UITextField ⽴立即顯⽰示輸⼊入⽂文字 • QV096:UITextField 的更多控制 • UIAlertView Delegate • QV098:處理 AlertView 上其他的按鈕 • UIActionSheet • QV044:UIActionSheet 由下往上彈出
  • 4. 協定的定義 • 表達物件的⾏行為與⽅方法宣合的集合稱為 協定,協定與實作會怎麼改變並沒有關 係,⽽而是以對應到何種訊息的⾓角度來看 待物件,並據以系統化的⽅方法。 • 善⽤用此⽅方法,便能定義出⾼高彈性與獨⽴立 性的類別。
  • 5. Objective-C 協定 • ⽅方法宣告的集合 • 如何實作交給各個類別來實現 • protocol 只宣告 method,也不實作 • protocol 將定義 method 內容的任務交給 答應遵守 protocol 的類別
  • 6. Objective-C 協定 (續) • 去實作某個協定的類別之間,彼此之間 有無繼承關係都無所謂 • 唯⼀一必要的,是協定中被宣告的⽅方法最 後必須在代理⼈人中被實作出來
  • 7. 協定的宣告 • 語法如下: @protocol 協定名稱 - ⽅方法 (method) 的宣告; - ⽅方法 (method) 的宣告; ............ @end
  • 8. 協定的採⽤用 • 語法如下: @interface 類別名稱 : 超類別 <協定名稱> { 實體屬性的宣告; ............ } - ⽅方法的宣告; ............ 可視為協定內的 @end ⽅方法已經宣告了
  • 9. 指定必要功能與選項功能 • 例如鬧鐘的協定: @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
  • 11. main.m (1/3) @protocol operation -(void) add:(int)n; -(void) sub:(int)n; -(void) mul:(int)n; -(void) div:(int)n; @end
  • 12. @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
  • 13. 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; }
  • 15. delegate • 代理⼈人:替當事⼈人分擔事務,協助完成 • delegate 定義了許多重要的 method (即為 protocol) • 在適當的時機對應的 method 即會被呼叫
  • 16. 想要有⼀一個『播放⾯面板』 具備功能 的元件可以直接使⽤用 希望可以藉此開發出下列應⽤用
  • 17.
  • 18.
  • 19. iOS 的代理機制 • 沒有 delegate,幾乎沒法寫程式... UIPickerView, UIScrollView, UITextView, UIWebView, UITableView, ...... • 程式的⽣生命週期.....AppDelegate
  • 20. ⼀一些註記 • 協定和繼承有相似的地⽅方,但協定只有 method,沒有 member • 繼承只能單⼀一繼承 (Objective-C 沒有多重繼承) • 參考 Java 的 Interface......
  • 24. 範例: ⽂文字輸⼊入框,即時顯⽰示已輸⼊入的字數 (並進⾏行輸⼊入的檢查) QV094:指定事件的⽅方式 (xib) QV095:指定事件的⽅方式 (coding) QV096:使⽤用 Delegate 的⽅方式
  • 25. Project QV094 ⽴立即顯⽰示輸⼊入⽂文字的數⺫⽬目 (使⽤用 UITextField) ⽅方法之⼀一:透過 xib 指定 『Editing Changed』事件 注意:Value Changed 無⽤用
  • 26. 設定⽂文字框輸⼊入鍵盤的隱藏 (2) 事件應改為 Did End On Exit (1) 預設的事件無 效,應刪除
  • 27. ⽂文字輸⼊入之後的處理事件 (2) 事件應改為 Editing Changed (1) 事件 Value Changed 無效
  • 28. 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 連接事件
  • 29. ViewController.m -(IBAction)doneEdit:(id)sender { [sender resignFirstResponder]; } -(IBAction)checkInput:(id)sender { int count = [userInput.text length]; displayLabel.text = [NSString stringWithFormat:@"%d", count]; } 顯⽰示已經輸⼊入的字數
  • 30. Project QV095 ⽴立即顯⽰示輸⼊入⽂文字的數⺫⽬目 (使⽤用 UITextField) ⽅方法之⼆二:事件的指定寫在 程式內 (addTarget)
  • 31. 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)
  • 32. -(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]; //錯的 }
  • 33. Project QV096 ⽴立即顯⽰示輸⼊入⽂文字的數⺫⽬目 (使⽤用 UITextField) 控制輸⼊入的內容 限定⻑⾧長度 只接收(禁⽌止) 指定的字元 ⽅方法之三:使⽤用 delegate ⽅方 式,實作 protocol
  • 34. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> { IBOutlet UILabel *displayLabel; IBOutlet UITextField *userInput; } @end (2) 指定使⽤用協定 UITextFieldDelegate (1) 不需要指定事件⽅方法 (不需開發者⾃自訂)
  • 35. 實作 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; }
  • 36. 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; }
  • 40. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UIAlertViewDelegate> - (IBAction) buttonClick:(id)sender; @end 指定使⽤用協定 UIAlertViewDelegate ViewController.xib
  • 41. 簡單的⽤用法 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]; }
  • 42. 另⼀一種⽤用法。⽽而且內部還有另⼀一個 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]; }
  • 47. 程序 在 ViewController.h 中增加協定 <UIActionSheetDelegate> 以便處理按鈕 事件 準備好執⾏行 ActionSheet 的起始按鈕及事 件⽅方法等,與 xib 連結 - (IBAction) openActionSheet:(id)sender 程式撰寫
  • 48. 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
  • 50. 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]; }
  • 51. //判斷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]; } }
  • 52. //將按鈕的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 = @"沒有處理這個按鍵"; }
  • 53. 很多按鈕項⺫⽬目 - (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]; }
  • 54. 簡單的畫⾯面 - (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]; }
  • 55. 參考資料 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