我在哪里可以指定哪个UIView用于UIViewController的view属性? [英] Where can I specify which UIView is used for the view property of a UIViewController?

查看:99
本文介绍了我在哪里可以指定哪个UIView用于UIViewController的view属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时设置UIViewController的view属性.我有一个带有两个视图的.xib文件,并且我希望拥有.xib文件的UIViewController子类决定运行时使用哪个UIView.我以为我可以在loadView中做到这一点,只需说一遍

I want to set the view property of a UIViewController at runtime. I have an .xib file with two views, and I want my UIViewController subclass that owns the .xib file to decide which UIView to use at runtime. I thought I could do this in loadView by just saying

if(some condition)
    self.view = thisView;
else
    self.view = thatView;

但是没有用.我该怎么办?

but that didn't work. How can I do this?

推荐答案

如果要动态选择视图,请将其设置在-[UIViewController loadView]内部.不过请注意:如果尚未加载视图,则调用-[UIViewController view]会调用-[UIViewController loadView],所以如果这样做:

If you want to choose your view dynamically, set it inside -[UIViewController loadView]. A word of caution though: calling -[UIViewController view] will call -[UIViewController loadView] if the view hasn't been loaded yet, so if you do this:

-(void)loadView
{
    self.view = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    self.view.backgroundColor = [UIColor redColor];
}

该方法的第二行将调用-loadView,您将获得无限递归(这将导致堆栈溢出和崩溃).您需要先设置视图,然后设置.view属性,如下所示:

The second line of that method will call -loadView, and you'll get infinite recursion (which will lead to a stack overflow, and a crash). You need to setup your view, then set the .view property when you've set it up, like this:

-(void)loadView
{
    UIView *newView =  [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    newView.backgroundColor = [UIColor redColor];

    self.view = newView;
}

所以您可能想要做这样的事情:

So you'll probably want to do something like this:

-(void)loadView
{
    UIView *newView = nil;

    if (self.theSkyIsBlue) {
        newView = [[[BlueSkyView alloc] initWithFrame:CGRectZero] autorelease];
        newView.backgroundColor = [UIColor blueColor];
    }
    else {
        newView = [[[GraySkyView alloc] initWithFrame:CGRectZero] autorelease];
        newView.backgroundColor = [UIColor grayColor];
    }

    self.view = newView;
}

附录1-更新以显示如何将容器视图用于XIB中定义的不同视图

如果要引用XIB中的其他内容,更好的方法是将.view用作其他视图的容器视图".像这样在-viewDidLoad中进行设置:

If you want to reference other stuff in your XIB, a better approach is to use your .view as a "container view" for your other views. Set it up in -viewDidLoad, like this:

- (void)viewDidLoad
{
    UIView *childView = nil;
    if (someCondition) {
        childView = self.blueView;
    }
    else {
        childView = self.grayView;
    }
    [self.view addSubview:childView];
    childView.frame = self.view.bounds;
}

请注意,如果以后要交换视图,应将childView设置为属性,而不是局部变量,以便在插入新的childView时将其删除.

Note that if you want to swap your views later on, you should make childView a property, instead of a local variable, so you can remove the old childView when inserting a new one.

这篇关于我在哪里可以指定哪个UIView用于UIViewController的view属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆