xcode 如何正确创建根视图控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7782796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:08:50  来源:igfitidea点击:

How to properly create a root view controller?

iphonexcodeuiviewcontrollerroot

提问by user278859

After upgrading to xCode 4.2 I am getting the following warning...

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

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.

在阅读了我在网上能找到的关于 RootViewController 的尽可能多的内容后,我不确定我是否正确地创建了我的根视图控制器。很久以前,当我第一次学习在 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...

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

@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.

无论如何,我将根视图控制器的名称更改为 RootViewController 并遵循我在cupsofcocoa.com 上找到的示例。但即使在更改之后,我仍然收到“......预计有一个根控制器......”警告。

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

回答by donjordano

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

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

把 return NO 放在 else 语句中,最后把 return nil; 希望这有帮助。

回答by jeet.chanchawat

Applications are expected to have a root view controller

应用程序应该有一个根视图控制器

Replace in AppDelegate

在 AppDelegate 中替换

 [window addSubview:[someController view]];

to

 [self.window setRootViewController:someController];