0

I have a UIViewController containing a UIImageView that has been correctly wired up (I can set image inside controller).

I have also set an iVar using

UIImageView *_imgQRCode;

and a property using

@property (strong, nonatomic) IBOutlet UIImageView *imgQRCode;

Synthesized using:

@synthesize imgQRCode = _imgQRCode;

But when I call something like below I nothing happens the image and when I inspect in Xcode the reference is memory reference is 0x00000000. note the code works when called inside the ViewController. The following is called from any other view controller with controller instantiated as follow:

QRPopupViewController *qrPop = [[QRPopupViewController alloc] initWithNibName:@"QRPopupViewController" bundle:nil];

[controller.imgView setImage:[UIImage imageNamed:@"sample.png"]];

Any ideas how to fix this and why i'm getting null?

jim
  • 8,670
  • 15
  • 78
  • 149

2 Answers2

2

Firsty: outlets should be weak

@property (weak, nonatomic) IBOutlet UIImageView *imgQRCode;

You have to hook imgQRCode outlet to your viewController, and you can use it inside viewDidLoad.

Michal Zaborowski
  • 5,039
  • 36
  • 35
0

You will get null for all UI Control's instances which are not part of the currently visible view controller. So due to this all your calls to that UI control will not perform the UI related operation.

If you want to update any UI control which is not part of currently visible view controller, you can use app delegate. It is the only place which is centralized in between all view controllers.

There is a mechnism, called "protocol delegate" to call some method to update UI part from the currently visible view controller. You can go through this URL for detail about this.

If you are selecting the approach of "protocol delegate", then explore the method execution flow in detail. When you call a protocol from a method of currently visible view controller, the corresponding protocol implementation method will be called after the completion of the caller method. So you need to take care of synchronization.

Community
  • 1
  • 1
sam18
  • 631
  • 1
  • 10
  • 24
  • I have just discovered that I needed to present the view before setting the image. This is the most relevant answer. Thanks. – jim Nov 29 '12 at 11:38