为什么这个iPhone程序不调用-loadView? [英] Why is this iPhone program not calling -loadView?

查看:110
本文介绍了为什么这个iPhone程序不调用-loadView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力通过基本的iPhone编程工作,我对Interface Builder的工作原理有了很好的基本了解,所以我决定尝试用编程方式处理视图。我已经通过ViewController苹果指南,搜索无处不在,我似乎找不到我的问题的解决方案。这让我相信这是一个非常简单的解决方案,但我只是把我的头撞在墙上。基本上所有我想做的是创建一个视图,获取主窗口作为子视图。我知道如果self.view没有定义,那么loadView方法应该被调用,一切都应该在那里设置。这是我的代码的当前状态:



委托:

   - (void)applicationDidFinishLaunching:(UIApplication *)application {
StartMenuViewController * aViewController = [[StartMenuViewController alloc] init];
self.myViewController = aViewController;
[aViewController release];

UIView * controllersView = [myViewController view];

window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen] .bounds];
[window setBackgroundColor:[UIColor redColor]];

[window addSubview:controllersView];
[window makeKeyAndVisible];
}

视图控制器:

   - (id)init {
if(self = [super init]){
self.title = @Start Menu;
}
return self;
}

//实现loadView以编程方式创建视图层次,而不使用nib。
- (void)loadView {
UIView * startView = [[UIView alloc] initWithFrame:[UIScreen mainScreen] .applicationFrame];
[startView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[startView setBackgroundColor:[UIColor greenColor]];
self.view = startView;
[startView release];感谢您的帮助!


你确定你是从UIViewController继承而不是覆盖 - (UIView *)视图的实现吗?



编辑:更多信息:



UIViewController有一个特殊的 - (UIView *消息,以便在调用时,如果未设置视图成员变量,则会调用loadView方法。所以,如果你在你的子类中提供了一个 - (id)view的实现(或者一个名为view的属性),它将打破 - loadView的自动调用。


I am trying to work my way through basic iPhone programming and I have a good basic understanding of how Interface Builder works, so I decided to try my hand at doing the views programmatically. I have gone through the ViewController Apple guide and searched everywhere and I cannot seem to find a solution to my problem. This leads me to believe it is a very simple solution, but I am just really banging my head against the wall. Basically all I am trying to do is create a view that gets main window as a subview. I know that if self.view is not defined then the loadView method is supposed to be called, and everything is supposed to be set up there. Here is the current state of my code:

The delegate:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    StartMenuViewController *aViewController = [[StartMenuViewController alloc] init];
    self.myViewController = aViewController;
    [aViewController release];

    UIView *controllersView = [myViewController view];

    window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [window setBackgroundColor:[UIColor redColor]];

    [window addSubview:controllersView];
    [window makeKeyAndVisible];
}

The view controller:

- (id)init {
    if (self = [super init]) {
        self.title = @"Start Menu";
    }
    return self;
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    UIView *startView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    [startView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
    [startView setBackgroundColor:[UIColor greenColor]];
    self.view = startView;
    [startView release];
}

Thanks for the help in advance!

解决方案

Are you sure that you're inheriting from UIViewController and not overriding the implementation of - (UIView*)view?

EDIT: More info:

UIViewController has a special implementation of the "-(UIView*) view" message so that when it's called, the loadView method is called if the view member variable is not set. So, if you provide an implementation of "- (id)view" in your subclass, (or a property named view) it will break the auto-calling of "- loadView".

这篇关于为什么这个iPhone程序不调用-loadView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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