0

我启用了 ARC,在我的didFinishLaunchingWithOptions方法中,我编写了以下代码:

AppDelegate.h:

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    ViewController * vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.viewController = nav;
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

但是语句:self.viewController = nav;得到一个编译警告,警告信息是:

file://.../AppDelegate.m: warning: Semantic Issue: Incompatible pointer types passing 'UINavigationController *__strong' to parameter of type 'ViewController *'

编译警告信息

如何删除警告?

谢谢。

4

4 回答 4

2

I assume that ViewController is a custom subclass of UIViewController which is either completely different or a subclass of UINavigationController itself. That's why it's wrong: a superclass can't completely act as it's subclass(es) (e. g., it may not have certain properties/methods etc.), hence the warning.

于 2012-04-07T08:13:06.257 回答
1

尝试以下代码: -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];

    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}
于 2012-04-07T08:42:11.113 回答
1

编译器告诉你:“导航,一个 UINavigationController 的实例,不是一个 'ViewController' 或 'ViewController' 的子类”。如果你真的想同时保留导航控制器和视图控制器,你可以添加第二个属性:

@property (nonatomic, strong) UINavigationController *navController;

然后将其设置在application:didFinishLaunchingWithOptions:

self.viewController = vc;
self.navController = nav;

这里的另一个解决方案是只保留导航控制器并使用“topViewController”属性来访问您的 VC。

编辑:或者更好的是,不关心导航控制器。只需这样做:

self.viewController = vc;
self.window.rootViewController = nav;
于 2012-04-07T08:12:04.050 回答
0

你可以这样做

 self.viewController =[nav.viewControllers objectAtIndex:0];

那么它不会显示类似“不兼容的指针类型将'UINavigationController *__strong'传递给'ViewController *类型的参数”的警告

于 2012-04-07T08:22:33.253 回答