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
See Question&Answers more detail:os