2010-07-16 19 views
4

我有一个简单的项目来呈现一个模态视图控制器并根据被按下的模态VC中的哪个按钮传回一个字符串。我全部基于观看iTunes U上的斯坦福大学课程。它看起来像我有一切正确,但我得到了一些编译器警告。实现模态视图控制器数据传输的委托方法

首先,我得到一个在TransferViewController.m

称为第二passing argument 1 of 'setDelegate:' from incompatible pointer type我送四个警告,叫Invalid receiver type 'id <MyModalViewControllerDelegate>*'但这些都不是在构建结果区MyModalViewController.m显示,而旁边的违规线,这两条线在每个按钮操作。

下面的代码...

// 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"]; 
    } 

} 

回答

4

获取id <something>和BEF后摆脱那些* S的矿石delegate

所以,再一次让这个

id <MyModalViewControllerDelegate> *delegate; 

id <MyModalViewControllerDelegate> delegate; 
+0

谢谢!当我说我理解使用委托的概念时,我想我在另一个问题上撒了谎,但我认为我现在就这样做了。当我在父VC中定义委托方法时,由于它在父代中实现,所以它可以访问父代中的ivars。当模态VC使用该方法时,它会获得回到父级的管道。 – Steve 2010-07-16 17:16:08

+0

关于无效类型问题,我认为这是Objective-C如此宽容的情况之一。很高兴让我错误地将委托定义为一个指针,但是当我真的试图以这种方式使用它时,我感到很沮丧 - 关于如何定义它的提示在编译器警告中会很好。也许在xCode 4! – Steve 2010-07-16 17:16:49

+0

是的,看起来像你得到它:) 一个委托仍然指向一个对象,所以如果你会说SomeDelegateClass *委托,你仍然需要*,事情是这个ID已经有这个*'内'它,所以你不需要明确写它。 – 2010-07-16 21:07:23

相关问题