我可能做得不对。在我做自己觉得自然的事情之前还没有做过iPad应用程序。
我有一个uiview,它将由其他uiview组成。具体地说,我希望动态地创建子视图并将其添加到每个子视图中。换言之:
UIView
|
| --> UIView
|
| --> Dynamic UIView 1
| --> Dynamic UIView 2
| --> ...
我通过在initWithFrame中添加这段代码来加载xib,从而获得了“动态uiview 1”:
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
[self addSubview:infoView];
它起作用了!我遇到的问题是“动态uiview n”总是呈现在屏幕的左上角。如果我试图用setframe改变位置,我会得到一个表示帧的黑框,但是实际的控件内容仍然在左上角。
有什么想法吗?有更好的方法吗?
-----更新-----
下面是根视图的屏幕截图。
Overview of main UIView http://www.freeimagehosting.net/uploads/460090996d.png
整个控件称为CurrentItemsView控制器。绿色区域是一个uiview,我通过ib中的一个链接在代码中链接回来,在代码示例中它被称为“itemsuiview”。以下是我如何在currentemsview controller.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;
}
在itemview.m中,我有这个,它只通过方法2和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;
}
如图所示,我尝试以3种不同的方式创建itemview,并且每个方法都呈现相同的结果。当我将视图添加为根视图的子视图时,我得到了这个。请注意,一个正方形出现在它应该呈现的地方,但是viewitem实际上在左上角呈现。在上面的示例中,当我添加SubitemView1时,它根本不呈现任何内容。
alt text http://www.freeimagehosting.net/uploads/c6635ce860.png