协议中的方法未在iOS中实现

Swr*_*r79 2 protocols ios5

我不明白,因为错误出现"协议中的方法没有实现"

myProtocol.h

#import <Foundation/Foundation.h>

@protocol myProtocol <NSObject>

-(UIImage *)transferImage;

@end
Run Code Online (Sandbox Code Playgroud)

ViewController.h

#import "SecondClass.h"

@interface ViewController : UIViewController<myProtocol, UINavigationControllerDelegate>

{
UIView *view;

}

@property (nonatomic,retain) UIImageView *imageView;

- (IBAction)sendImage:(id)sender;

@end
Run Code Online (Sandbox Code Playgroud)

ViewController.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];
Run Code Online (Sandbox Code Playgroud)

secClass.delegate = self; [secClass callTransfer]; NSLog(@"我在发件人中"); [self.navigationController pushViewController:secClass animated:YES];}

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

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
Run Code Online (Sandbox Code Playgroud)

SecondViewController.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
Run Code Online (Sandbox Code Playgroud)

Ale*_*Cio 5

你在delegate里面调用方法,SecondViewController但你没有插入它.如果你得到一个像... not implemented它刚才所说的警告,你就忘记了一个方法.你可以像这样插入方法

-(UIImage *)transferImage{
    //do something here if delegate has been called
}
Run Code Online (Sandbox Code Playgroud)

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

@optional
Run Code Online (Sandbox Code Playgroud)

如果不指定,则所有方法都将设置为@requiredinitial.