Implementing delegate methods for modal view controller data transfer
I have a simple project to present a modal view controller and transfer back a string based on which button in the modal VC that gets pressed. I based it all on watching the Stanford class on iTunes U. It looks like I have everything correct, but I get a couple of compiler warnings.
First I get one called passing argument 1 of 'setDelegate:' from incompatible pointer type
in TransferViewController.m
Second I get four warnings called Invalid receiver type 'id <MyModalViewControllerDelegate>*'
but these aren't displayed in the build results area, rather next to the offending lines in MyModalViewController.m
, both lines in each of the button actions.
Here's the code...
// TransferViewController.h
#import <UIKit/UIKit.h>
#import "MyModalViewController.h";
@interface TransferViewController : UIViewController <MyModalViewControllerDelegate> {
UILabel *label;
UIButton *button;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) UIButton *button;
- (IBAction)updateText;
@end
// TransferViewController.m
#import "TransferViewController.h"
@implementation TransferViewController
@synthesize label;
@synthesize button;
- (IBAction)updateText {
MyModalViewController *myModalViewController = [[MyModalViewController alloc] init];
myModalViewController.delegate = self; // I get the warning here.
[self presentModalViewController:myModalViewController animated:YES];
[myModalViewController release];
}
- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog {
label.text = selectedDog;
[self dismissModalViewControllerAnimated:YES];
}
@end
// MyModalViewController.h
#import <UIKit/UIKit.h>
@protocol MyModalViewControllerDelegate;
@interface MyModalViewController : UIViewController {
UIButton *abby;
UIButton *zion;
id <MyModalViewControllerDelegate> delegate;
}
@property (assign) id <MyModalViewControllerDelegate> delegate;
- (IBAction)selectedAbby;
- (IBAction)selectedZion;
@end
@protocol MyModalViewControllerDelegate <NSObject>
@optional
- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog;
@end
// MyModalViewController.m
#import "MyModalViewController.h"
@implementation MyModalViewController
@synthesize delegate;
- (IBAction)selectedAbby {
if ([self开发者_Go百科.delegate respondsToSelector:@selector (myModalViewController:didFinishSelecting:)]) {
[self.delegate myModalViewController:self didFinishSelecting:@"Abby"];
}
}
- (IBAction)selectedZion {
if ([self.delegate respondsToSelector:@selector (myModalViewController:didFinishSelecting:)]) {
[self.delegate myModalViewController:self didFinishSelecting:@"Zion"];
}
}
Get rid of those *
s after id <something>
and before delegate
.
So make this
id <MyModalViewControllerDelegate> *delegate;
this
id <MyModalViewControllerDelegate> delegate;
精彩评论