为什么这个iPhone程序不调用

编程入门 行业动态 更新时间:2024-10-23 03:32:15
本文介绍了为什么这个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程序不调用

本文发布于:2023-10-28 19:21:28,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   iPhone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!