• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

加载视图控制器和amp;amp;查看层次编程在Cocoa Touch没有xib

用户头像
it1352
帮助1

问题说明

似乎所有的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.

正确答案

#1

完成程序化用户界面生成相当简单。首先,你需要编辑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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhccjjki
系列文章
更多 icon
同类精品
更多 icon
继续加载