简单代表示例?

时间:2009-06-27 00:43:30

标签: objective-c delegates implementation

好的,我正在使用Objective-C编程并使用Xcode。我已经阅读了Apple网站上的文档并了解了代理人的内容,但是当我谈到如何将代理方法实际实现到代码中时,我只是感到困惑,特别是当他们说“现在实现委托”时方法。”也许它只是我,但我不知道确切地在哪里实现该方法(在我只有ViewController和AppDelegate类的简单情况下,AppDelegate.h / .m文件是否是正确的位置?)。我想我学习的最好方法就是看一个非常简单的例子。

我下面有一些代码,我想知道是否有人可以通过并告诉我如何将委托连接到ViewController,以便显示总和?很抱歉,如果代码看起来很长,但这是我能想到的最简单的委托示例。为了争论和查看更少的代码(让我更容易看到发生的事情),假设ServerClass *服务器实现了一个服务器而ClientClass *客户端实现了一个客户端。两者已经相互连接,正在等待输入他们的号码。我放下了我认为正确的内容,但我知道它确实不完整(只要将委托连接到服务器和客户端)。我不知道放在哪里的是协议声明,所以如果有人能够做到这个简单的问题,那么就学习如何将一个委托实现到一个类中,它会帮助我很多。

顺便说一句,我正在使用iPhone SDK 3.0的新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

2 个答案:

答案 0 :(得分:12)

我不会对您发布的代码进行任何详细分析 - 对于超越特定代码示例的一般原则,您可以获得的最有用的响应是一些方向。以下是一般原则......

  • 委托是一个对象,其对象(通常)被调用以处理或响应特定事件或操作。
  • 您必须“告诉”一个接受您希望成为委托的委托的对象。这可以通过在您的代码中调用 [object setDelegate:self]; 或设置 object.delegate = self; 来完成。
  • 充当委托的对象应实现指定的委托方法。该对象通常在协议中定义方法,或者在NSObject上通过类别定义为默认/空方法,或两者​​。 (正式的协议方法可能更清晰,特别是现在Objective-C 2.0支持可选的协议方法。)
  • 当发生相关事件时,调用对象检查委托是否实现匹配方法(使用-respondsToSelector:)并调用该方法(如果有)。然后,代理人可以控制在将控制权返回给调用者之前做必要的响应。

在您正在完成的具体示例中,请注意GKPeerPickerController有一个名为 delegate 的属性,该属性接受 id<GKPeerPickerControllerDelegate>类型的对象即可。这意味着id(NSObject的任何子类)实现GKPeerPickerControllerDelegate协议中的方法。 GKPeerPickerControllerDelegate反过来定义了许多委托方法,并描述了它们何时被调用。如果您实现这些方法中的一个或多个(文档说所有方法都是可选的,但需要两个)并注册为委托,那么将调用这些方法。 (请注意,您不需要在.h文件中声明方法原型,只需导入协议标头并在.m文件中实现该方法。

答案 1 :(得分:1)

我正在学习ObjC和iPhone开发。到目前为止,我不会说我完全了解代表及其使用。在Apple网站上的开发人员门户网站上找到的Your First iPhone Application详细介绍了一个非常简单的示例,它使用TextField的委托覆盖一个方法,使键盘在完成TextField编辑时消失。例如,如果我可以从那里粘贴相关的片段:

// 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

在此处,textFieldShouldReturnUITextFieldDelegate协议的一部分。尽管我已经理解了,但重要的是,无论您在哪个类中实现委托方法,该类必须遵循该特定委托的协议(通过将尖括号中的协议名称紧跟在旁边它继承的类的名称)。

相关问题