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

前端之家收集整理的这篇文章主要介绍了iphone – 为模态视图控制器数据传输实现委托方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单的项目来呈现一个模态视图控制器,并根据按下的模式VC中的哪个按钮传回一个字符串.我基于在iTunes U上观看Stanford课程.我看起来一切都正确,但我得到了一些编译器警告.

首先,我从TransferViewController.m中的不兼容指针类型中得到一个名为’setDelegate:’的传递参数1

其次,我得到四个警告称为无效接收器类型’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"];
    }

}

解决方法

在id< something>之后摆脱那些* s在代表之前.

所以说吧

id <MyModalViewControllerDelegate> *delegate;

这个

id <MyModalViewControllerDelegate> delegate;

猜你在找的Xcode相关文章