问题描述
好吧,我在Objective-C中编程并使用Xcode。我已经阅读了苹果网站上的文档,并了解了哪些代表,但是当我谈到如何将代理方法实际代码化的部分时,我只是感到困惑,特别是当他们说现在执行代理方法。也许这只是我,但我不知道WHERE实现方法(AppDelegate.h / .m文件在简单的情况下,我只有ViewController和AppDelegate类的正确位置)。我想我真的最好的办法是看一个很简单的例子。我下面有一些代码,我想知道有人可以浏览并告诉我如何将代理连接到ViewController,以显示总和?对不起,如果代码看起来很长,但这是我可以想到的最简单的代理示例。为了争论和减少代码(使我更容易看到发生了什么),让我们说ServerClass *服务器实现一个服务器,ClientClass *客户端实现一个客户端。两者都已经相互连接,并且正在等待输入他们的号码。我放下了我认为是正确的,但我知道这是不完整的(就连接代理服务器和客户端)。有一件事我不知道该放在哪里是协议声明,所以如果有人能够做这个简单的问题,那么就可以帮助我了解一个代表如何实现到一个类中。
顺便说一下,我正在与iPhone SDK 3.0的新GameKit中的Peer Picker合作,如果有人也想向我显示什么连接到什么。例如,我在具有名为 代表
,它接受一个类型为 id< GKPeerPickerControllerDelegate>
的对象。这意味着实现 GKPeerPickerControllerDelegate
协议中的方法的 id
(NSObject的任何子类)。 反过来定义了一些委托方法,并描述何时他们会被打来。如果您实现了这些方法中的一个或多个(文档说所有都是可选的,但有两个方法),并注册为委托,这些方法将被调用。 (请注意,您不需要在.h文件中声明方法原型,只需导入协议头并实现.m文件中的方法。
Ok, I'm programming in objective-C and using Xcode. I have read through the documentation on Apple's website and understand what delegate's are but when I come to the part where it talks about how to actually implement delegate methods into code, I just become confused, especially when they say something like "now implement the delegate's method." Maybe it's just me, but I don't know exactly WHERE to implement the method (would the AppDelegate.h/.m file be the correct location in a simple situation where I only have the ViewController and AppDelegate class?). I guess truly the best way for me to learn is to see a very simple example.
I've got some code below and I was wondering if someone could go through and show me how to connect the delegate to the ViewController so that it shows the sum? Sorry if the code looks long, but this is the simplest delegation example I could think of. For the sake of argument and having less code to look at (making it easier for me to see what's happening), lets say ServerClass *server implements a server and ClientClass *client implements a client. Both are already connected to each other and are waiting to input their number. I put down what I think would be correct but I know for sure it's not complete (as far as connecting the delegate to both server and client). One thing I don't know where to put are the protocol declarations, so if someone could please do this simple problem, it would help me out a lot as far as learning how a delegate is implemented into a class.
By the way, I'm working with the Peer Picker in the new GameKit of the iPhone SDK 3.0 if someone would also like to show me what connects to what. For example, I am at step 3 of the Apple guide for Peer Picker. Now, I don't know where step 5 goes in my project. Thanks to all who can help me understand this delegate implementation...you all have been great so far!
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
I won't go into any detailed analysis of the code you posted — the most helpful response you could get is some direction as far as general principles that transcend a specific code sample. Here are the general principles...
- A delegate is an object whose objects are (generally) called to handle or respond to specific events or actions.
- You must "tell" an object which accepts a delegate that you want to be the delegate. This is done by calling
[object setDelegate:self];
or settingobject.delegate = self;
in your code. - The object acting as the delegate should implement the specified delegate methods. The object often defines methods either in a protocol, or on NSObject via a category as default/empty methods, or both. (The formal protocol approach is probably cleaner, especially now that Objective-C 2.0 supports optional protocol methods.)
- When a relevant event occurs, the calling object checks to see if the delegate implements the matching method (using
-respondsToSelector:
) and calls that method if it does. The delegate then has control to do whatever it must to respond before returning control to the caller.
In the specific example you're working through, notice that GKPeerPickerController has a property named delegate
which accepts an object of type id<GKPeerPickerControllerDelegate>
. This means an id
(any subclass of NSObject) that implements the methods in the GKPeerPickerControllerDelegate
protocol. GKPeerPickerControllerDelegate in turn defines a number of delegate methods and describes when they will be called. If you implement one or more of those methods (the documentation says all are optional, but two are expected) and register as a delegate, those methods will be called. (Note that you don't need to declare a method prototype in your .h file, just import the protocol header and implement the method in your .m file.
这篇关于简单的代理示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!