1

I am trying to create a UIImageView but I have to make it programmatically and I have to be able to declare it with an instance variable (in the .h file or something of the sort). Here is the code for creating it; however, this does not allow me to use it in other methods.

UIImageView *airImage = [[UIImageView alloc] 
                            initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:airImage];

I have looked on other people asking similar questions however none of them will allow me to create an instance variable. BTW that code is in my viewDidLoad. Thanks in advance!

Wayne
  • 59,728
  • 15
  • 131
  • 126

3 Answers3

1

In your .h use:

UIImageView *airImage;

In your viewDidLoad:

airImage=[[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:airImage];

Or you can declare it as a property:

@property (nonatomic, strong) UIImageView *airImage;

and use to access it:

self.airImage = [[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:self.airImage];
PCoder123
  • 364
  • 2
  • 11
  • You should mention as in previous answers, whether to declare it as a property, or ivar. – Milo Mar 19 '14 at 00:50
0

To be more specific, instance variables should be created in a specific place in an interface (can be both in your .h and .m files, but use .h as it is more common).

If you want to declare it in your .h file, then you will want your code to look like this:

@interface ClassName : UIViewController {
    UIImageView *_airImage; //many developers use _ to represent ivars
}

@end

To set the value of the variable, then you can use

_airImage = [[UIImageView alloc]init...];

Property's are another option. Instead, you can declare this like so:

@interface ClassName : UIViewController

@property (strong, nonatomic) UIImageView *airImage;

@end

To set this value, simply use,

self.airImage = [[UIImageView alloc]init...];

Hope this helped clear some things up. Use this question to help understand the difference and when to use ivars vs properties: What is the difference between ivars and properties in Objective-C

This tutorial shows how you can use both ivars and properties together, and just help you understand them both better: http://www.icodeblog.com/2011/07/13/coding-conventions-ivars/

Community
  • 1
  • 1
erdekhayser
  • 6,537
  • 2
  • 37
  • 69
  • You should add that if it's a property, it can be accessed from any file, ivars are local to that file. – Milo Mar 19 '14 at 00:50
  • Thanks for helping clear this up! When I get a chance I will go through those tutorials. I am a bit of a noob when it comes to terminology. –  Mar 19 '14 at 00:51
  • @MiloGosnell The tutorials I linked cover this. I feel that it may be out of the scope of the question, e.g. creating instance variable/property – erdekhayser Mar 19 '14 at 00:53
0

in your .h

@property (nonatomic, strong) UIImageView *airImage; // public

in your .m (viewDidLoad or wherever you want to init your ImageView)

self.airImage = [[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:self.airImage];
Fede Cugliandolo
  • 1,686
  • 15
  • 24