2012-04-07 66 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; 
} 
相關問題