2009-06-27 49 views
8

好吧,我正在編程Objective-C和使用Xcode。我已閱讀了Apple網站上的文檔,瞭解代理的內容,但是當我談到如何將代理方法實際實現爲代碼時,我只是感到困惑,特別是當他們說「像現在實現代理方法。」也許這只是我,但我不知道確切的WHERE實現方法(在我只有ViewController和AppDelegate類的簡單情況下,AppDelegate.h/.m文件是否是正確的位置?)。我想我真正想學習的最好方法就是看一個非常簡單的例子。簡單代表示例?

我在下面有一些代碼,我想知道如果有人可以通過並告訴我如何將委託連接到ViewController,以便它顯示總和?對不起,如果代碼看起來很長,但這是我能想到的最簡單的代表性例子。爲了爭論和少看代碼(使我更容易看到發生的事情),可以說ServerClass *服務器實現了一個服務器,而ClientClass *客戶端實現了一個客戶端。兩者已經相互連接並等待輸入他們的號碼。我放下了我認爲正確的東西,但我確信它不完整(只要將代理連接到服務器和客戶端)。有一件事我不知道該把協議聲明放在哪裏,所以如果有人可以請做這個簡單的問題,那麼就可以瞭解一個委託如何實現到一個類中。

順便說一下,我正在與iPhone Picker的新GameKit中的Peer Picker合作,如果有人也想告訴我什麼是連接到什麼的。例如,我在step 3 of the Apple guide for Peer Picker。現在,我不知道步驟5在我的項目中的位置。感謝所有能夠幫助我理解這個代表實現的人......迄今爲止,你們都非常棒!

ExampleAppDelegate.h

#import <UIKit/UIKit.h> 

@class ExampleAppViewController; 

@interface ExampleAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    ExampleAppViewController *viewController; 
    int sum; 
} 

@property (nonatomic, retain) sum; 
@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet ExampleAppViewController *viewController; 

-(void) addNum:(int)num; 
@end 

ExampleAppDelegate.m

#import "ExampleAppDelegate.h" 
#import "ExampleAppViewController.h" 

@implementation ExampleAppDelegate 

@synthesize window; 
@synthesize viewController; 


- (void)applicationDidFinishLaunching:(UIApplication *)application {  

    application.idleTimerDisabled = YES; 

    // Override point for customization after app launch  
    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 
} 


- (void)dealloc { 
    [viewController release]; 
    [window release]; 
    [super dealloc]; 
} 

-(void)addNum:(int)num { 
    sum += num; 
} 

@end 

ExampleAppViewController.h

#import <UIKit/UIKit.h> 
#import <GameKit/GameKit.h> 

@interface ExampleAppViewcontroller : NSObject { 
     IBOutlet UILabel *sumField; // will display the total sum, one number entered //by the server and one entered by the client, on both iPhones after calculation 

     int sum; // the total sum after addition; 
     ServerClass *server; // some server 
     ClientClass *client; // some client 
     int num; // the number to add to sum 
} 

@property(nonatomic, assign) sum; 
@property(nonatomic, retain) num; 

-(void) displaySum; 
@end 

ExampleAppViewController.m

#import "ExampleAppViewcontroller.h" 

@implementation ExampleAppViewController 

@synthesize sum; 
@synthesize num; 

-(void) displaySum { 
    [sumfield setText: @"%i", sum]; 
} 

@end 
+0

如果在我的代碼中的一些語法問題的話,那是因爲我現在不是在我的Mac,我必須鍵入了一個基於Windows的PC上。 – 2009-06-27 01:06:54

+0

我讀過關於語法的評論,但如果你不知道,我相信你不需要(非原子,保留)一個整數。據我所知,我可能是錯的,我從我見過的代碼的基礎上,以及你不保留整數的事實作爲基礎。只要做@property總和; – 2009-06-27 02:04:45

+0

我認爲首選的物業組合是@property(非原子,分配)的事情是不是對象的指針(整數,布爾值將等) – Tim 2009-06-27 03:09:35

回答

12

我不會對你發佈的代碼進行任何詳細的分析 - 你可以得到的最有用的迴應是一些方向,就像超越特定代碼示例的一般原則一樣。以下是一般原理...

  • 委託是一個對象(通常)被調用來處理或響應特定事件或動作的對象。
  • 你必須「告訴」一個接受你想成爲委託的委託的對象。這是通過在您的代碼中調用[object setDelegate:self];或設置object.delegate = self;完成的。
  • 充當代表的對象應實現指定的委託方法。該對象通常在協議中定義方法,或者在NSObject上通過類別定義爲默認/空方法或兩者。 (正式協議方法可能更清晰,尤其是現在Objective-C 2.0支持可選的協議方法。)
  • 當發生相關事件時,調用對象將檢查代理是否實現匹配方法(使用-respondsToSelector:)並調用該方法,如果它。然後,代表在控制權返回給調用者之前已經有了控制權去做任何必須做出的響應。

在你工作過,具體的例子注意到GKPeerPickerController有一個名爲delegate屬性,它接受類型id<GKPeerPickerControllerDelegate>的對象。這意味着id(NSObject的任何子類)實現GKPeerPickerControllerDelegate協議中的方法。 GKPeerPickerControllerDelegate依次定義了一些委託方法,並描述它們何時被調用。如果你實現了一個或多個這些方法(文檔說所有都是可選的,但有兩個是預期的)並且註冊爲一個委託,那麼這些方法將被調用。 (請注意,您不需要在您的.h文件中聲明的方法的原型,只是導入協議頭,並實現你的.m文件的方法。

1

我學習ObjC和iPhone的發展。我不會達到迄今最高至可以說,我理解代表和他們的使用完美。Your First iPhone Application,對蘋果公司的網站開發人員門戶網站發現,走過了詳細一個很簡單的例子,它利用了文本字段的委託的覆蓋,使鍵盤的方法。在文本字段編輯完成例如,當消失,如果我可以從那裏粘貼相關片段:

// MyViewController.h 
#import <UIKit/UIKit.h> 

@interface MyViewController : UIViewController <UITextFieldDelegate> { 
     UITextField *textField; 
     UILabel *label; 
     NSString *string; 
} 

@property (nonatomic, retain) IBOutlet UITextField *textField; 
@property (nonatomic, retain) IBOutlet UILabel *label; 
@property (nonatomic, copy) IBOutlet NSString *string; 

- (IBAction)changeGreeting:(id)sender; 

@end 


// MyViewController.m 
#import "MyViewController.h" 

@implementation MyViewController 

@synthesize textField; 
@synthesize label; 
@synthesize string; 

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { 
     if (theTextField == textField) { 
       [textField resignFirstResponder]; 
     } 
     return YES; 
} 

@end 

在這裏,textFieldShouldReturn是的0部件的方法協議。雖然我已經明白了,什麼是重要的是,無論哪級你實現委託方法,該類必須遵循特定委託的協議(具有尖括號括起來緊鄰類的名稱協議名稱它繼承自)。