我是目标 C 的初学者。我想将图像从ViewController
to传递SecondClass
给以显示SecondClass
已经在ViewController
. 但是传输后的传输imageview
显示值,按下按钮后null
我无法到达。secondClass
我的代码如下,
我的协议.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
第二类.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
第二类.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