我是目标C的初学者。我想将ViewController
的图片传递给SecondClass
,以便在SecondClass
中显示ViewController
中的图片。但转移的imageview
转移后显示null
值,按下按钮后无法到达secondClass
。我的代码在下面,
myProtocol.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 "SecondClass.h"
#import "myProtocol.h"
(void)viewDidLoad
{
[super viewDidLoad];
imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"photo1.png"]];
[view addSubview:imageView];
NSLog(@"I am in VC.m");
}
-(UIImage *)transferImage{
NSLog(@"I am in transferImage");
return imageView.image;
}
- (IBAction)sendImage:(id)sender {
SecondClass *secClass = [[SecondClass alloc]init];
secClass.delegate=self;
[secClass callTransfer];
NSLog(@"I am in sender");
[self.navigationController pushViewController:secClass animated:YES];
}
@end
SecondClass.h
#import "myProtocol.h"
#import "ViewController.h"
@interface SecondClass : UIViewController<myProtocol,UINavigationControllerDelegate>
{
UIView *secondView;
IBOutlet UIImageView *myImage;
id <myProtocol> myDelegate;
}
@property (nonatomic,retain) UIImageView *myImage;
@property(nonatomic,assign) id delegate;
-(void)callTransfer;
@end
SecondClass.m
#import "SecondClass.h"
#import "ViewController.h"
#import "myProtocol.h"
@implementation SecondClass
@synthesize delegate,myImage;
- (void)viewDidLoad
{
[super viewDidLoad];
[secondView addSubview:myImage];
}
-(void)callTransfer
{
myImage.image=[delegate performSelector:@selector(transferImage)];
myImage.image=[UIImage imageNamed:@"photo1.png"];
NSLog(@"%@",myImage.image);
NSLog(@"I am in call transfer");
}
@end
答案 0 :(得分:1)
使用此代码并检查
UIImage *img = [delegate performSelector:@selector(transferImage)];
NSLog(@"%@",img );
NSLog(@"I am in call transfer");
答案 1 :(得分:0)
在这里你不需要代理,你大大过分复杂了将数据传递给新视图控制器的简单任务。
SecondClass *secClass = [[SecondClass alloc] init];
secClass.myImage.image = imageView.image;
[self.navigationController pushViewController:secClass animated:YES];
你提到这是为了练习 - 但它仍然是根本错误的。对于委托来说,更常见的情况是从child -> parent
发送图像而不是从parent -> child
发送图像,因为父级拥有子级,并且可以在没有精心设置协议的情况下调用方法。
#import "ChildVC.h"
@interface ParentVC : UIViewController <ChildDelegate>
@end
@implementation ParentVC
- (void)pushViewController;
{
ChildVC *childVC = [[ChildVC alloc] init];
childVC.delegate = self;
[self.navigationController pushViewController:childVC animated:YES];
}
- (void)childViewController:(ChildVC *)childViewController didFinishWithImage:(UIImage *)image;
{
// do something with the image passed back
}
@end
然后孩子看起来像
@class ChildVC;
@protocol ChildDelegate <NSObject>
- (void)childViewController:(ChildVC *)childViewController didFinishWithImage:(UIImage *)image;
@end
@interface ChildVC : UIViewController
@property (nonatomic, weak) id<ChildDelegate> delegate;
@property (nonatomic, strong) UIImage *image;
@end
@implementation ChildVC
- (void)whenSomethingInterestingHappens;
{
if ([self.delegate respondsToSelector:@selector(childViewController:didFinishWithImage:)]) {
[self.delegate childViewController:self didFinishWithImage:self.image];
}
[self.navigationController popViewControllerAnimated:YES];
}
@end
答案 2 :(得分:0)
how to pass two view controller image for delegate method :
//step 1: create for delegate method :
@protocol DrawViewDelegate
-(void)didReceiveDrawImage:(UIImage *)DrawImage;
@end
step2: delegate pass for your first view:
@interface EditorViewController :
UIViewController< DrawViewDelegate >
// step3:你必须是时间的视图控制器传递视图
like : crop.delegat=self;
// step:4传递第二个视图控制器协议
@protocol DrawViewDelegate;
//步骤:5使用方法设置图像传递其他视图:
[_delegate didReceiveDrawImage:savedimage];
//step: 6 get second view image to first view
-(void)didReceiveDrawImage:(UIImage *)DrawImage