Xcode 4.2中的多个视图

时间:2012-02-14 03:06:22

标签: objective-c ios5 xcode4.2

我在查找Xcode 4.2中没有故事板的多个视图的教程时遇到了很多麻烦,这是一个类,所以我还不能使用storyboard。我只是想在主视图中单击一个按钮时出现第二个带有UIPicker的视图,我只是找不到这个版本的Xcode,它与旧版本的不同,让我感到困惑。< / p>

任何帮助表示如果有人能够快速描述我需要做什么或更新的教程我会很感激:)

1 个答案:

答案 0 :(得分:4)

我认为您应该阅读UIView Programming Guide以便更好地了解UIView的工作方式。我发现nibs / storyboard非常适合混淆新的iOS开发人员。

从本质上讲,UIViewController有1个视图,您可以使用viewDidLoadloadView[self setView:someUIView]方法中设置该视图。通过添加UIView s作为viewcontroller的“Main”视图的子视图,可以向屏幕添加更多内容。例如

-(void)loadView {
    // Create a view for you view controller
    UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self setView:mainView];

    // Now we have a view taking up the whole screen and we can add stuff to it
    // Let's try a button, a UIButton is a subview of UIView
    UIButton *newButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
    // We need to set a frame for any view we add so that we specify where it should
    // be located and how big it should be!
    [newButton setFrame:CGRectMake(x,y,width,height)];
    // Now let's add it to our view controller's view
    [self.view addSubview:newButton];

    // You can do the same with any UIView subclasses you make!
    MyView *myView = [[MyView alloc] init];
    [myView setFrame:CGRectMake(x,y,width,height)];
    [self.view addSubview:myView];

}

现在我们有了viewController,他们的视图只是一个简单的UIView,而后者又有2个子视图; newButton和myView。由于我们创建了MyView类,也许它也包含子视图!让我们来看看UIView子类的外观:

// Here is the init method for our UIView subclass
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Let's add a button to our view
        UIButton *newButton2 = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
        // Of course, the frame is in reference to this view
        [newButton2 setFrame:CGRectMake(x,y,width,height)];
        // We add just using self NOT self.view because here, we are the view!
        [self addSubview:newButton2];

    }
    return self;
}    

所以在这个例子中我们有一个视图控制器,他们现在看到包含2个按钮!但视图结构是一棵树:

           mainView
          /      \
  newButton     myView
                  \
                newButton2

如果您有任何其他问题,请与我们联系!

马特