PDA

View Full Version : Using an object method to display a UIImageView in another objects .xib




NickFalk
Feb 28, 2009, 05:24 PM
Phew, quite a headline. Anyway, I may be way off track here but what I am trying to achieve is to have an instance of the object "Room" output to a nib called "DungeonView.xib" (being controlled by "DungeonViewController").

Room.h
#import "Room.h"

@implementation Room
@synthesize floor;

-(void)displayRoomFloor:(id)sender
{
NSLog(@"displayRoomFloor");
floor.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"floor1.png"],[UIImage imageNamed:@"floor2.png"], nil];
floor.animationDuration = 0.2;
floor.animationRepeatCount = 0;
[floor startAnimating];
}
@end

I've added an Object to the DungeonView.xib and changed its type to "Room".
I've hooked up the Room Class' IBOutlet "floor" to a UIImageView in the DungeonView.xib

It seems to me that the problem has likely to do with where the Room object (or rather its instance) is outputting. I believe this as the animation bit works perfectly on its own when it is included directly in my DungeonViewController in the "viewDidLoad" method. I am now instead trying the following to achieve the same functionality from the instance of "Room".

DungeonViewController
-(void)viewDidLoad {
[super viewDidLoad];
room = [[[Room alloc] init] autorelease];
[room displayRoomFloor:self];
}



I also know that the method is called as the console displays the NSLog call just fine. Anyone?



eddietr
Feb 28, 2009, 06:43 PM
In viewDidLoad, you are creating a new instance of Room. That instance is not the same instance (not the same Room object) as the one in your nib.

Hope that helps.

NickFalk
Mar 1, 2009, 02:19 AM
Hi Eddie and thank you for your feedback.

I am quite aware that the object instance "room" in my code and the "Room" object in the xib are not the same object. Thanks for the effort though! :)

I am still at a loss when it comes to hooking up an IBOutlet from any instance of another object than the current ViewController. Perhaps it is just not possible? I've asked this question over several different forums, including the Developer Forums at Apple and at least the answer isn't obvious as you are the first guy to actually reply.

Oh, guess I'll have to rethink my strategy. Perhaps it is better to have the Room object return an animation and then actually execute that animation through the UIViewController...

eddietr
Mar 1, 2009, 06:57 AM
I guess I'm still having a hard time understanding exactly what you are asking, then.

So are you saying that you have a nib which contains a UIImageView and you want to set that UIImageView as the outlet of an object that is not in that same nib (and is also not the file's owner)?

If that's what you're asking, then probably the most straightforward way would be to get that UIImageView* from DungeonViewController at runtime.

NickFalk
Mar 1, 2009, 11:54 AM
Hi again Eddie. I've gone down another path for now, but yes you've got the zest of it.

I actually think my problem might have been that I attacked the problem from the wrong angle. Rather than doing as you suggested in your last post (passing the UIImageView too the "Room" object instance, I tried to return another UIImageView (including animating images) from the Room instance to my viewController.

I'll see later on if I'll bother to get my original idea to work. Thanks again!