如何正确创建根视图控制器? [英] How to properly create a root view controller?

查看:75
本文介绍了如何正确创建根视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到xCode 4.2后,我收到以下警告...

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

在阅读了有关RootViewController的所有内容后,我不确定是否已正确创建了根视图控制器.很久以前,当我第一次学习使用xCode编程时,就创建了它.

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.

我有一个问题是可以将根视图控制器命名为RootViewController以外的名称.我现在看到的每个示例都将其命名为RootViewController.我也看到它像这样在应用程序委托中综合了...

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;

无论如何,我都将根视图控制器的名称更改为RootViewController,并按照在 cupsofcocoa上找到的示例进行操作. com .但是即使进行了更改,我仍然会收到"...预计将具有root控制器..."警告.

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.

谢谢

约翰

  //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;
}

推荐答案

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([launchOptions count] == 0) {
        _rootViewController = [[RootViewController alloc] init];
        self.window.rootViewController = self.rootViewController;
        **[window addSubview:_rootViewController.view];**
        [window makeKeyAndVisible];
        return YES;

    }else{
        [JLHelper showAlertWithTitle:@"" message:[NSString stringWithFormat:@"launchOptions: %@", launchOptions]];
 return NO;
    }
return nil;
}

在else语句中放入return NO,最后将return nil放入;希望有帮助.

Put return NO inside else statement and on the end put return nil; Hope this help.

这篇关于如何正确创建根视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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