2012-04-07 42 views
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 *'

Compile Warning Information

如何删除警告?

谢谢。

回答

2

我认为视图控制器是UIViewController中的自定义子类,或者是完全不同的的UINavigationController本身的子类。这就是为什么它是错误的:超类不能完全作为其子类(例如,它可能没有某些属性/方法等),因此是警告。

1

编译器告诉你:“nav,一个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; 
0

,你可以做这样的

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

话,就不会显示出像

1

尝试“过客‘的UINavigationController * __强’到类型的参数“视图控制器*不兼容的指针类型”的警示以下代码: -

- (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; 
} 
相关问题