My task is to set background image in UIImageView in SecondViewController by clicking button on the FirstViewController.
FirstViewController.h :
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@class SecondViewController;
@interface ViewController : UIViewController
{
SecondViewController *secondViewData;
}
// pointer to second view
@property (nonatomic, strong) SecondViewController *secondViewData;
// buttons
- (IBAction)changeBack:(id)sender;
@end
button method in FirstViewController.m :
- (IBAction)changeBack:(id)sender {
SecondViewController *svc = [[SecondViewController alloc] init];
self.secondViewData = svc;
svc.back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:svc animated:YES completion:nil];
}
SecondViewController.h :
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface SecondViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *back;
@end
back - it's a IBOutlet UIImageView in SecondViewController. Also I imported FirstViewController.h in SecondViewController.m. I tried to pass string to SecondViewController - and it's logged fine in -ViewDidLoad in SecondViewController, but can't set UIImageView.
back
, I'd suggest having a property for the image (not the imageview) and have theviewDidLoad
ofSecondViewController
set the imageview to use the image property you set here. Generally, you shouldn't be messing around with controls beforeviewDidLoad
since the view for the view controller probably hasn't even been created yet. – Rob