gpt4 book ai didi

ios5 - 协议(protocol)中的方法未在 iOS 中实现

转载 作者:行者123 更新时间:2023-12-05 01:19:34 25 4
gpt4 key购买 nike

我不明白,因为错误出现“Method in protocol not implemented”

我的协议(protocol).h

#import <Foundation/Foundation.h>

@protocol myProtocol <NSObject>

-(UIImage *)transferImage;

@end

ViewController.h

#import "SecondClass.h"

@interface ViewController : UIViewController<myProtocol, UINavigationControllerDelegate>

{
UIView *view;

}

@property (nonatomic,retain) UIImageView *imageView;

- (IBAction)sendImage:(id)sender;

@end

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];

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

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

最佳答案

您在 SecondViewController 中调用了 delegate 方法,但您没有插入它。如果你收到类似 ... not implemented 的警告,它只是说你忘记包含一个方法。你可以像这样插入方法

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

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

@optional

如果不指定,所有方法都将设置为@required initial。

关于ios5 - 协议(protocol)中的方法未在 iOS 中实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19346877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com