views:

117

answers:

1

I'm having trouble getting my SubViews properly initialized using Interface Builder. I have the following View hierarchy (UIWindow -> BlankCanvasView -> StalkerView). BlankCanvasView is a subclass of UIView and StalkerView is a IBOutlet of BlankCanvasView

@interface BlankCanvasView : UIView {

IBOutlet UIView *stalker;

}

@end

I've established a connection between the stalker outlet of BlankCanvasView and the subview. However, in my touchesBegin method of BlankCanvasView the stalker outlet is nil. See below for touchesBegin.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSLog(@"Touch Begin detected!!!");
NSLog(@"Stalker instance %@", stalker);

[UIView beginAnimations:@"StalkerAnimation" context:nil];

UITouch *touch = [touches anyObject];

    //stalker is nil here!!!
[stalker setCenter:[touch previousLocationInView:self]];

[UIView commitAnimations];

}

What am I missing? It looks like none of my demo apps are properly loading any subviews when I try and follow examples on iTunesU.

A: 

If you are creating your 'stalker' view in IB, it must be part of the view hierarchy (that is, added as a subview of another view), or it must be retained by your code in order for it to not be released after loading. If you use a retain property for your stalker variable, this will be taken care of for you, automatically:

@interface BlankCanvasView : UIView
{
    UIView *stalker;
}

@property (nonatomic, retain) IBOutlet UIView *stalker;

@end

Make sure you @synthesize stalker; in your BlankCanvasView's implementation, and set the property to nil when you're deallocating your view:

@implementation BlankCanvasView
@synthesize stalker;

- (void)dealloc
{
    self.stalker = nil;
    [super dealloc];
}

@end
PfhorSlayer
Hi Slayer,I've actually tried it both ways with just an iVar and a property with retain semantics. In either case, the property or iVar is nil. Looking at other similiar postings on this topic, I wonder if I need to override the awakeFromNib method of the UIView class?
lcranf