2

我不明白,因为错误出现“协议中的方法未实现”

我的协议.h

#import <Foundation/Foundation.h>

@protocol myProtocol <NSObject>

-(UIImage *)transferImage;

@end

视图控制器.h

#import "SecondClass.h"

@interface ViewController : UIViewController<myProtocol, UINavigationControllerDelegate>

{
UIView *view;

}

@property (nonatomic,retain) UIImageView *imageView;

- (IBAction)sendImage:(id)sender;

@end

视图控制器.m

#import "ViewController.h"
#import "SecondViewController.h"
#import "myProtocol.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];

_imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"VoodooVibe@2x.png"]];

[view addSubview:_imageView];

NSLog(@"I am in VC.m");
}

-(UIImage *)transferImage{
NSLog(@"I am in transferImage");
return _imageView.image;}
- (IBAction)sendImage:(id)sender {
SecondViewController *secClass = [[SecondViewController alloc]init];

secClass.delegate=self;[secClass callTransfer];NSLog(@"I am in sender");[self.navigationController pushViewController:secClass animated:YES];}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

SecondViewController.h

#import <UIKit/UIKit.h>

#import "myProtocol.h"

#import "ViewController.h"

@interface SecondViewController :               UIViewController<myProtocol,UINavigationControllerDelegate> {

UIView *secondView;

IBOutlet UIImageView *myImage;

id <myProtocol> myDelegate;
}

@property (nonatomic,strong) UIImageView *myImage;

@property(nonatomic,weak) id delegate;

-(void)callTransfer;

@end

第二视图控制器.m

#import "SecondViewController.h"

#import "ViewController.h"

#import "myProtocol.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize delegate,myImage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

[secondView addSubview:myImage];
}
-(void)callTransfer

{
myImage.image=[delegate performSelector:@selector(transferImage)];

myImage.image=[UIImage imageNamed:@"VoodooVibe@2x.png"];

NSLog(@"%@",myImage.image);

NSLog(@"I am in call transfer");

}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
4

2 回答 2

5

您在内部调用该delegate方法,SecondViewController但没有插入它。如果您收到... not implemented刚刚所说的警告,则说明您忘记包含方法。你可以插入这样的方法

-(UIImage *)transferImage{
    //do something here if delegate has been called
}

或者您只需在delegate块内的方法上方添加一个参数:

@optional

如果您不指定它,所有方法都将设置为@required初始。

于 2013-10-13T16:14:50.580 回答
3

它就像您在协议实现中声明了一个方法但没有实现一样简单。在您的第二个 VC 中,您还没有实现“传输图像”方法。所以编译器正在生成警告

于 2013-10-14T07:28:24.380 回答