user546459
user546459

Reputation: 569

Issue Subclassing UIView

I've created my own custom view, with its own header and main file and corresponding nib (.xib):

The header file

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface PointsBarView : UIView 
{
    IBOutlet UIView *pointsBarView; 
    IBOutlet UIView *pointsCounterView; 
    IBOutlet UILabel *pointsTotalLabel;
    UIImageView *handImageView;
    UIImageView *powerBarOutlineImageView;
}

@property (nonatomic, retain) IBOutlet UIView *pointsCounterView;   
@property (nonatomic, retain) IBOutlet UIView *pointsBarView;
@property (nonatomic, retain) IBOutlet UILabel *pointsTotalLabel;
@property (nonatomic, retain) IBOutlet UIImageView *handImageView;
@property (nonatomic, retain) IBOutlet UIImageView *powerBarOutlineImageView;

@end

I'm synthesizing everything in the main, and then in another UIViewController class I'm trying to load this view. I set up its property:

@property (nonatomic, assign) IBOutlet PointsBarView *pointsBarView;

And am adding as so:

NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"PointsBarView" owner:self options:nil];
    pointsBarView = [nibViews objectAtIndex: 0];    

    [[self view] addSubview:pointsBarView];

How do I access those subviews within my NIB? Do they need to be embedded within the pointsBarView? (pointsBarView references the main view of the NIB, and all the other views are within the pointsBarView). Should they each be a separate piece of the NIB and I need to call addSubview for each one to display?

I should note that if I do NOT connect any of the properties in PointsBarView, the view displays just fine with the code in my UIViewController class. But I want to be able to interact with each view and change properties accordingly. Any help would be great!

Upvotes: 0

Views: 939

Answers (1)

amattn
amattn

Reputation: 10065

The general rule of thumb is: if you load it from code, you connect it up in code.

conversely:

If you instantiate in IB, you can connect outlets and actions in IB.

Here you are loading the view in code, so you have to manually connect them.

If you want to be able to connect stuff in IB add a UIVIew in IB and change the subclass to PointsBarView. IB will magically read the PointsBarView.h file and you should be able to connect outlets, targets and actions.

Upvotes: 1

Related Questions