8

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

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

顺便说一句,如果有人还想向我展示什么连接到什么,我正在使用 iPhone SDK 3.0 的新 GameKit 中的 Peer Picker。例如,我在Peer Picker 的 Apple 指南的第 3 步。现在,我不知道第 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
4

2 回答 2

11

我不会对你发布的代码进行任何详细的分析——你能得到的最有用的回应是一些关于超越特定代码示例的一般原则的方向。以下是一般原则...

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

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

于 2009-06-27T05:24:31.397 回答
1

我正在学习 ObjC 和 iPhone 开发。我不会说我完全理解代表及其使用。您的第一个 iPhone 应用程序,可在 Apple 网站的开发人员门户上找到,详细介绍了一个非常简单的示例,该示例使用 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

Over here, textFieldShouldReturn is a method that is part of the UITextFieldDelegate protocol. As much as I have understood it, what is important is that whichever class you implement the delegate methods in, that class must follow that particular delegate's protocol (by having a protocol name enclosed in angle brackets immediately next to the name of the class it inherits from).

于 2009-06-27T05:58:22.583 回答