【问题标题】:How to properly create a root view controller?如何正确创建根视图控制器?
【发布时间】:2011-10-16 06:40:21
【问题描述】:

升级到 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;
}

【问题讨论】:

    标签: iphone xcode uiviewcontroller root


    【解决方案1】:
    - (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;
    }
    

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

    【讨论】:

    • 如果你想支持 ios
    • 实际上我确实有添加Subview的行。我尝试返回 nil 但它会引发警告“指向整数转换的指针不兼容......”。无论如何,我仍然在运行时收到“应用程序应该有一个根视图控制器......”警告。
    【解决方案2】:

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

    在 AppDelegate 中替换

     [window addSubview:[someController view]];
    

     [self.window setRootViewController:someController];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      相关资源
      最近更新 更多