【问题标题】:how can i create multiple view on a view如何在一个视图上创建多个视图
【发布时间】:2011-09-11 08:17:51
【问题描述】:

我有两个班级

  1. view1 类。(从使用基于视图的应用程序创建项目,背景为灰色)
  2. view2 类。(来自为 view1、view2、view3 和 view4 添加文件 uiviewcontroller 子类)

在 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/

view2http://postimage.org/image/2mqj0gnj8/

谢谢

【问题讨论】:

  • 所以你想要它作为 view1 图片??
  • 别忘了接受其中一位回答者 ;)

标签: iphone objective-c uiview


【解决方案1】:

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

[self.view addSubView:sub];

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

【讨论】:

  • 谢谢我找到的每一个人 [code] v1 addSubView:sub.view;很有用。
【解决方案2】:

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];

}

【讨论】:

  • 在 self.view 上面有 4 个视图我想在 self.view 中没有的 4 个视图上显示 view2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
  • 2013-11-30
相关资源
最近更新 更多