在SecondViewController中单击button时,在ViewController中显示图像

如何通过单击另一个UIViewControllerUIButton来显示UIIimage ? 我想添加到相同的UIButton命令添加到SecondViewController的图像。 请原谅我这个可怜的问题。

myProtocol.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,retain) UIImageView *myImage; @property(nonatomic,assign) 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 

通常应该使用委托,如果你有一个包含在你的UIViewController的类,它会做某些事情,并在特定过程完成时用委托方法通知你。 但在这种情况下,你会设置一个UIImage两次。 一旦由您的delegate和第二次以编程方式设置UIImage

你不应该像从外部调用一个方法来初始化第二个UIViewControllerUIImage 。 只需调用viewDidLoad所有东西,而不必关心它,因为UIViewController自己处理它。

你只需要在你的SecondViewController里面插入一个UIImageView并把它连接到你的头文件。 那么你可以在里面访问它。 文件。 我有问题,我第一次使用一个jpg而不是一个png ,但更改后缀一切工作正常。

ViewController.m

 - (IBAction)sendImage:(id)sender { SecondViewController *secClass = [[SecondViewController alloc] init]; [secClass setImageName:@"pic.png"]; [self.navigationController pushViewController:secClass animated:YES]; } 

SecondViewController.h

 @interface SecondViewController : UIViewController @property (strong, nonatomic)NSString *imageName; @property (weak, nonatomic) IBOutlet UIImageView *myImage; @end 

SecondViewController.m (只有这两行)

 - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [_myImage setImage:[UIImage imageNamed:_imageName]]; }