私は Objective 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