tags:

views:

290

answers:

1

I have created a UIViewController class called MyViewController with a UIImageView in its XIB file. I then import this class into another class. I make an instance of the class and I change the image in the UIImageView using code:

myViewController.myImageView.image = [UIImage imageNamed:@"myImage.png"];

This works swimmingly. My app is essentially an image viewer. I wanted to cache next and previous images by preloading a subview with an image. When I place myViewController into a variable like this:

UIViewController *pager = myViewController;

And attempt to use the variable to set the image for the UIImageView like this:

pager.myImageView.image = [UIImage imageNamed:@"myImage.png"];

I get an "error: request for member 'myImageView' in something not a structure or union". I've tried doing it using square brackets:

[[[pager myImageView] setImage:[UIImage imageNamed:@"myImage.png"]];

I get "warning 'UIViewController' may not respond to '-myImageView' ". How do I access the hierarchy to get to myImageView? I've used a @class in the header and a #import and I've synthesized the class instance. The only solution I have so far is a hack, by doing this:

UIImageView *pager = myViewController.myImageView;
pager.image = [UIImage imageNamed:@"myImage.png"];

Which doubles the amount of variables I have, one for the UIImageView variable, and one for the UIViewController variable. Thanks in advance to anyone that can help out.

+1  A: 

quick answer:

You'll need to change the line:

UIViewController *pager = myViewController;

to:

MyViewController *pager = myViewController;

To be able to access the custom properties you added in your MyViewController class.

explanation:

Your MyViewController class probably looks something like this (simplified):

// MyViewController.h:
@interface MyViewController : UIViewController
{
    UIImageView * myImageView;
}
@property (readonly) UIImageView * myImageView;
@end

// MyViewController.m:
@implementation MyViewController
@synthesize myImageView;
@end

You have extended the UIViewController class by adding a member variable and a property (myImageView) to the original class.

When you use a pointer to the base class (UIViewController) to access your variable, you are telling the compiler to treat that variable as if it was a plain old UIViewController, and a plain old UIViewController has no concept of a myImageView. This is what causes the compiler errors.

e.James
Thanks eJames for taking the time to answer, I figured same, may I further the question? My initial problem was using an array to hold pointers to my classes that are the same as above. Accessing the array using [myArray objectAtIndex:0] to get to the classes also yields the same unwanted results as the above. Would you know the solution to my problem? I can't specify a class constructor to stick the class pointer into the array. Thanks again!
DK Crame
Just be sure to use a MyViewController * to get the objects out of the array: MyViewController * item = [myArray objectAtIndex:0]; item.myImageView.image = ...
e.James
or, ((MyViewController *)[myArray objectAtIndex:0]).myImageView.image = ...
e.James
Thanks! Much appreciated I didn't think casting would work in the same way. Odd that objects are defaulted to its base class when you stick it into an array...
DK Crame
No worries. It's not that they are cast back to their base class, it just that NSArray objects are built to hold *any* object, so the object that you get out of an array is simply of type 'id'. The compiler has no idea how to treat it until you cast it to the appropriate type.
e.James
That was my last conceptual hurdle to Cocoa Touch. Again thanks for taking the time to answer, it's much appreciated.
DK Crame
You're welcome. Have fun with the coding :)
e.James