Show image in ViewController when button is clicked in SecondViewController

自作多情 提交于 2019-12-04 22:04:56

Delegates should normally be used, if you you have a class included inside your UIViewController which will do something and notify you with the delegate method when the specific process has finished. But in this case you would set an UIImage two times. Once by your delegate and a second time by setting an UIImage programmatically.

You shouldn't do anything like calling a method for initializing the UIImage of the second UIViewController from outside. Just call everything inside viewDidLoad and you don't have to care about it, because the UIViewController handles it itself.

You just have to insert an UIImageView inside your SecondViewController and connect it to your header file. then you can access it inside the m. file. I had the problem that I first used a jpg instead of a png, but after changing the suffix everything worked fine.

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 (There are just these two lines)

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [_myImage setImage:[UIImage imageNamed:_imageName]];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!