加载视图控制器&查看层次编程在Cocoa Touch没有xib [英] Loading a view controller & view hierarchy programatically in Cocoa Touch without xib

查看:112
本文介绍了加载视图控制器&查看层次编程在Cocoa Touch没有xib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎所有的Cocoa Touch模板都设置为加载一个nib。

It seems like all of the Cocoa Touch templates are set up to load a nib.

如果我想开始一个新的项目,将使用一个视图控制器,并加载其视图(层次结构)编程,而不是从nib / xib,步骤来设置或调整模板。

If I want to start a new project that's going to use a view controller, and load its view(hierarchy) programatically, not from a nib/xib, what are the steps to setting that up or adjusting a template.

我虽然所有我必须做的是实现-loadView,但我有麻烦每次我尝试这样做。 / p>

I though all I had to do was implement -loadView, but I have trouble every time I try to do this.

推荐答案

完成程序化用户界面生成相当简单。首先,你需要编辑main.m看起来像下面这样:

It's reasonably simple to do completely programmatic user interface generation. First, you need to edit main.m to look something like the following:

int main(int argc, char *argv[]) 
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];  
    UIApplicationMain(argc, argv, nil, @"MyAppDelegate");
    [pool release];
    return 0;   
}

其中MyAppDelegate是应用程序委托类的名称。这意味着MyAppDelegate的实例将在启动时创建,通常由应用程序的主要Nib文件处理。

where MyAppDelegate is the name of your application delegate class. This means that an instance of MyAppDelegate will be created on launch, something that is normally handled by the main Nib file for the application.

在MyAppDelegate中,实现您的applicationDidFinishLaunching:方法类似于以下内容:

Within MyAppDelegate, implement your applicationDidFinishLaunching: method similar to the following:

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{    
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    if (!window) 
    {
        [self release];
        return;
    }
    window.backgroundColor = [UIColor whiteColor];

    rootController = [[MyRootViewController alloc] init];

    [window addSubview:rootController.view];
    [window makeKeyAndVisible];
    [window layoutSubviews];    
}

其中MyRootViewController是窗口中主视图的视图控制器。这应该初始化主窗口,并将由MyRootViewController管理的视图添加到它。 rootController被保存为代理中的一个实例变量,供以后引用。

where MyRootViewController is the view controller for the primary view in your window. This should initialize the main window, and add the view managed by MyRootViewController to it. rootController is kept as an instance variable within the delegate, for later reference.

这样可以通过MyRootViewController以编程方式生成用户界面。

This should let you programmatically generate your user interface through MyRootViewController.

这篇关于加载视图控制器&查看层次编程在Cocoa Touch没有xib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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