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

发布于 2024-12-10 07:41:40 字数 2531 浏览 0 评论 0原文

升级到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金橙橙 2024-12-17 07:41:40
- (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;希望这有帮助。

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

触ぅ动初心 2024-12-17 07:41:40

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

将 AppDelegate 中的替换

 [window addSubview:[someController view]];

 [self.window setRootViewController:someController];

Applications are expected to have a root view controller

Replace in AppDelegate

 [window addSubview:[someController view]];

to

 [self.window setRootViewController:someController];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文