Tell me, there is a ViewController with a button and an ImageView. When the button is clicked in the ImageView, a picture is downloaded from the web. The question is how to show this picture in another ViewController? Thank.

View1.h

@interface asdViewController : UIViewController { IBOutlet UIButton *actBtnGet; } @property (strong, nonatomic) IBOutlet UIImageView *ImageView; @end 

View1.m

 #import "asdViewController.h" @interface asdViewController () @end @implementation asdViewController - (IBAction)actBtnGet:(id)sender { id path = @"http://host.ru/123.png"; NSURL *url = [NSURL URLWithString:path]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc] initWithData:data]; self.ImageView.image=img; } - (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. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end 
  • one
    @slake, if you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - AlessandroDP

1 answer 1

Eh, and so ??

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"TwoVCSeque"]) { TwoViewController *twoVC = (TwoViewController *)[segue destinationViewController]; twoVC.img = self.img; } } 

The question seems to be either too simple or I do not understand you))

In general, the main thing here is:

 twoVC.img = self.img; 

The rest you realize, as you need, in general, then ...

  • It's a shame, but I blunted at this place) We just learn on the sly) Thank you)) - slake
  • @slake, always please;) - AlessandroDP
  • There is such a thing, xcode swears at this line 'twoVC.img = self.img; '. Says "Use of undeclared identifier 'img'". Why? Is this an 'img' in the button or not? - slake
  • @slake, @property (strong, nonatomic) UIImage * img; And in the button you need to fix self.img = [[UIImage alloc] initWithData: data]; dropbox.com/s/wl3yitdknoq6nus/… - AlessandroDP
  • All figured out, thanks)) - slake