问题描述
我有一个简单的项目来呈现模态视图控制器并根据模态 VC 中的哪个按钮被按下来传回一个字符串.这一切都是基于在 iTunes U 上观看斯坦福课程.看起来我一切都正确,但我收到了一些编译器警告.
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.
首先我得到一个名为 passing argument 1 of 'setDelegate:' from incompatible pointer type
in TransferViewController.m
First I get one called passing argument 1 of 'setDelegate:' from incompatible pointer type
in TransferViewController.m
其次,我收到四个警告,称为 Invalid receiver type 'id <MyModalViewControllerDelegate>*'
但这些未显示在构建结果区域中,而是显示在 MyModalViewController 中的违规行旁边.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.
这是代码...
// 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.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"];
}
}
推荐答案
去掉id <something>
之后delegate*
/代码>.
做这个
id <MyModalViewControllerDelegate> *delegate;
这个
id <MyModalViewControllerDelegate> delegate;
这篇关于为模态视图控制器数据传输实现委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!