加载视图控制器在没有Xib的Cocoa Touch中以编程方式查看层次结构 [英] Loading a view controller & view hierarchy programmatically in Cocoa Touch without xib

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

问题描述

似乎所有的Cocoa Touch模板都已设置为加载笔尖。

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.

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

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