views:

61

answers:

1

I'm probably not doing this correctly. Having not done an iPad app before I'm doing what seems natural to me.

I have a UIView that is going to be made of up other UIViews. Specifically, I want to dynamically create and add subviews to each of these subviews. In other words:

UIView
   |
   | --> UIView
           |
           | --> Dynamic UIView 1
           | --> Dynamic UIView 2
           | --> ...

I managed to get 'Dynamic UIView 1' by adding to its initWithFrame this bit of code to load the xib:

NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView" 
                                                  owner:[[ItemViewController alloc] init]
                                                options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
[self addSubview:infoView];

It works! The problem I'm having is that 'Dynamic UIView n' is always rendered at the top left corner of the screen. If I try to change the position with setFrame I get a black box at representing the frame however the actual control content remains in the top left.

Any ideas? Is there a better way to do this?

----- Update -----

Here is a screenshot of the root view. Overview of main UIView

The entire control is call CurrentItemsViewController. The green area is a UIView within that I have linked back in code via a link in the IB, in the code sample it is called "itemsUIView". Here is how I am adding items within CurrentItemsViewController.m

- (id)init {
[super initWithNibName:nil
                bundle:nil];

UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:@"Current"];

/* Approach 1 */
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView" 
                                                  owner:[[ItemViewController alloc] init]
                                                options:nil];
ItemView *subItemView1 = [nibViews objectAtIndex:0];

/* Approach 2 - this apprach does Approach 1 in the initWithFrame method */
//ItemView *subItemView1 = [[ItemView alloc] initWithFrame:CGRectMake(120, 120, 354, 229)];

/* Approach 3 */
//ItemViewController *ctr = [[ItemViewController alloc] init];
//UIView *subItemView1 = [ctr view];

[[self view] addSubview:subItemView1];
// [[self itemsUIView] addSubview:subItemView1]; <-- doesn't render subItemView1 at all
[subItemView1 release];

[subItemView1 setFrame:CGRectMake(120, 120, 354, 229)];
[subItemView1 setBounds:CGRectMake(120, 120, 354, 229)];

return self;
}

Inside of ItemView.m I have this, it only gets call by Approach 2 and 3.

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {

    NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView" 
                                                      owner:[[ItemViewController alloc] init]
                                                    options:nil];
    ItemView *infoView = [nibViews objectAtIndex:0];
    [self addSubview:infoView];

}
return self;
}

As shown, I tried creating the ItemView 3 different ways and each one renders the same result. When I add the view as a subview of the root view I get this. Notice that a square appears where it should render, however ViewItem actually renders at the top left corner. In the sample above, when I add subItemView1 it doesn't render anything at all.

alt text

A: 

Try setting the frame before you add it as a subview.

Your code:

NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView" 
                                              owner:[[ItemViewController alloc] init]
                                            options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
self addSubview:infoView];

Should be:

NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView" 
                                              owner:[[ItemViewController alloc] init]
                                            options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
infoView.frame = CGRectMake( put your rect here );
[self addSubview:infoView];
NSArray