实现模态视图控制器数据传输的委托方法

时间:2010-07-16 16:32:47

标签: iphone xcode delegates modal-dialog

我有一个简单的项目来呈现一个模态视图控制器,并根据按下的模式VC中的哪个按钮传回一个字符串。我基于在iTunes U上观看斯坦福大学课程。我看起来一切都正确,但我收到了一些编译器警告。

首先,我在passing argument 1 of 'setDelegate:' from incompatible pointer type

中收到一个名为TransferViewController.m的广告

其次,我收到四个名为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"];
    }

}

1 个答案:

答案 0 :(得分:4)

*之后和id <something>之前摆脱delegate个。

所以做这个

id <MyModalViewControllerDelegate> *delegate;

id <MyModalViewControllerDelegate> delegate;