tags:

views:

48

answers:

1

Hi, I'm new to iPhone dev and need some help with adding subViews.

I have a reusable object that I made that is stored in a separate .h .m and xib file. I would like to use this object in my main project's view controller. I have included the header and the assignment of the object generates no errors. I am able to load the object into my main project but can only do things with it inside my viewDidLoad method. I intend to have a few of these objects on my screen and am looking fora solution that is more robust then just hard wiring up multiple copies of the shape object.

As soon as I try to access the object outside of the viewDidLoad it produces a variable unknown error - first use in this function.

Here is my viewDidLoad method:

shapeViewController *shapeView = [[shapeViewController alloc] initWithNibName:@"shapeViewController" bundle:nil];

[self.view addSubview: shapeView.view];

// This is the problem line // This code works changes the display on the shape object

[shapeView updateDisplay:@"123456"];

---- but the same code outside of the viewDidLoad generates the error.

So to sum up, everything works except when I try to access the shapeView object in the rest of the methods.

Thanks in advance

+1  A: 

You need to declare the shapeView instance in your interface, not just inside one function. Then the code in the function becomes just an initialization.

So, in your .h file, inside the @interface you write the declaration:

shapeViewController *shapeView;

And in your viewDidLoad, you will initialize what you declared before:

shapeView = [[shapeViewController alloc] initWithNibName:@"shapeViewController" bundle:nil];
[self.view addSubview:shapeView.view];

Now you can use shapeView in your entire class.

ivans
Hi Ivans, Thanks for the input. When I declare the shapeView instance in my interface I get the error "Local declaration of 'shapeView' hides instance variable.
DecodingSand
Edited for clarity.
ivans
Thanks Ivan, That worked!
DecodingSand
You could accept the answer then, eh?
ivans
@ivans, Don't you mention `dealloc` or using property? I hope you don't want @DecodingSand to face memory leak...
Michael Kessler