如何正确创建根视图控制器?
升级到 xCode 4.2 后,我收到以下警告...
应用程序预计在应用程序启动结束时有一个根视图控制器
在阅读了尽可能多的关于 RootViewController 的在线内容后,我不确定是否已正确创建根视图控制器。很久以前我第一次学习 xCode 编程时就创建了它。
我的一个问题是,是否可以将根视图控制器命名为 RootViewController 以外的名称。我现在看到的每个示例都将其命名为 RootViewController。我还看到它在应用程序委托中合成,如下所示...
@synthesize rootViewController = _rootViewController;
我不明白这是在做什么。为什么不只是......
@synthesize rootViewController;
无论如何,我将根视图控制器的名称更改为 RootViewController 并按照我在 cupsofcocoa.com。但即使在更改之后,我仍然收到“...预计有一个根控制器...”警告。
如果有人有时间看一下并让我知道我缺少什么,我在下面列出了初始化代码的重要部分。
谢谢,
约翰
//RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController {
}
@end
。
//RootViewController.m
#import "RootViewController.h"
#import "JetLoggerAppDelegate.h"
@implementation RootViewController
@end
。
//JetLoggerAppDelegate.h my app delegate
#import <UIKit/UIKit.h>
@class RootViewController;
@interface JetLoggerAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@end
。
//.m app delegate
#import "JetLoggerAppDelegate.h"
#import "RootViewController.h" //I don't think I need this here
@implementation JetLoggerAppDelegate
@synthesize window;
@synthesize rootViewController = _rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([launchOptions count] == 0) {
_rootViewController = [[RootViewController alloc] init];
self.window.rootViewController = self.rootViewController;
[window makeKeyAndVisible];
return YES;
}else{
[JLHelper showAlertWithTitle:@"" message:[NSString stringWithFormat:@"launchOptions: %@", launchOptions]];
}
return NO;
}
。
//main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"JetLoggerAppDelegate");
[pool release];
return retVal;
}
After upgrading to xCode 4.2 I am getting the following warning...
Applications are expected to have a root view controller at the end of application launch
After reading as much as I could find on line about the RootViewController I am not sure whether I have created my root view controller properly. I created it a long time ago when I was first learning to program in xCode.
One question I have is it ok to name the root view controller something other than RootViewController. Every example I see now has it named RootViewController. I also see it synthesized in the app delegate like this...
@synthesize rootViewController = _rootViewController;
I do not understand what this is doing. Why not just...
@synthesize rootViewController;
In any event I changed the name of my root view controller to RootViewController and followed the example I found at cupsofcocoa.com. But even after the changes I am still getting the "...expected to have a root controller..." warning.
If someone has the time to take a look and let me know what I am missing, I have listed the the significant portions of my initialization code below.
Thanks,
John
//RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController {
}
@end
.
//RootViewController.m
#import "RootViewController.h"
#import "JetLoggerAppDelegate.h"
@implementation RootViewController
@end
.
//JetLoggerAppDelegate.h my app delegate
#import <UIKit/UIKit.h>
@class RootViewController;
@interface JetLoggerAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@end
.
//.m app delegate
#import "JetLoggerAppDelegate.h"
#import "RootViewController.h" //I don't think I need this here
@implementation JetLoggerAppDelegate
@synthesize window;
@synthesize rootViewController = _rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([launchOptions count] == 0) {
_rootViewController = [[RootViewController alloc] init];
self.window.rootViewController = self.rootViewController;
[window makeKeyAndVisible];
return YES;
}else{
[JLHelper showAlertWithTitle:@"" message:[NSString stringWithFormat:@"launchOptions: %@", launchOptions]];
}
return NO;
}
.
//main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"JetLoggerAppDelegate");
[pool release];
return retVal;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 return NO 放在 else 语句中,最后放置 return nil;希望这有帮助。
Put return NO inside else statement and on the end put return nil; Hope this help.
应用程序应该有一个根视图控制器,
将 AppDelegate 中的替换
为
Applications are expected to have a root view controller
Replace in AppDelegate
to