如何在视图上创建多个视图

时间:2011-09-11 08:17:51

标签: iphone objective-c uiview

我有2个课程

  1. class view1。(来自基于视图的应用程序的创建项目背景中的灰色)
  2. class view2。(来自view1,view2,view3和view4的add file uiviewcontroller子类)
  3. 在类view1中创建4 uiview变量v1,v2,v3,v4并将其链接到视图1,视图2,视图3和视图4

    在View1的viewdidload中

    我代码

    view2 *sub =[[view2 alloc] initWithNibName:@"view2" bundle:nil];
    self.v1 = sub.view; (v1,v2,v3,v4 is view that i draw by interface builder on self.view)
    

    但在运行我创建的view2时没有出现;

    我怎样才能让它出现。

    view1 http://postimage.org/image/2mqhcxb1g/

    view2 http://postimage.org/image/2mqj0gnj8/

    谢谢

2 个答案:

答案 0 :(得分:2)

现在您只需将该视图分配给变量,以使视图可见,您应该执行以下操作:

[self.view addSubView:sub];

这会将视图添加到当前视图中并使其可见。

答案 1 :(得分:1)

以下内容将出现在viewDidLoad中:(仅适用于view2)

View2 *view2 = [[View2 alloc]
initWithNibName:@"View2" bundle:nil];

[self.view insertSubview:view2.view atIndex:0];
[view2 release];
[super viewDidLoad];

尝试为此设置框架以设置在欲望的位置。对其他3个观点也一样......

修改

- (void)loadView
{
// Create the main view
CGRect appRect = [[UIScreen mainScreen] applicationFrame];
contentView = [[UIView alloc] initWithFrame:appRect];
contentView.backgroundColor = [UIColor whiteColor];

// Provide support for autorotation and resizing
contentView.autoresizesSubviews = YES;
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
[contentView release];

// reset the origin point for subviews. The new origin is 0,0
appRect.origin = CGPointMake(0.0f, 0.0f);
// Add the subviews, each stepped by 32 pixels on each side
UIView *subview = [[UIView alloc] initWithFrame:CGRectInset(appRect, 32.0f, 32.0f)];
subview.backgroundColor = [UIColor lightGrayColor];
[contentView addSubview:subview];
[subview release];

subview = [[UIView alloc] initWithFrame:CGRectInset(appRect, 64.0f, 64.0f)];
subview.backgroundColor = [UIColor darkGrayColor];
[contentView addSubview:subview];
[subview release];

subview = [[UIView alloc] initWithFrame:CGRectInset(appRect, 96.0f, 96.0f)];
subview.backgroundColor = [UIColor blackColor];
[contentView addSubview:subview];
[subview release];

}