2011-12-29 69 views
4

我正在開發使用最新SDK的iOS 4應用程序,XCode 4.2ARC沒有已知的選擇器實例方法

我添加了一個方法來appDelegate.h

#import <UIKit/UIKit.h> 

@class ViewController; 
@class SecondViewController; 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
{ 
    UINavigationController* navController; 
    ViewController* viewController; 
    SecondViewController* secondViewController; 
} 

@property (strong, nonatomic) UIWindow *window; 

- (void) showSecondViewController; 

@end 

及其在appDelegate.m

#import "AppDelegate.h" 

#import "ViewController.h" 
#import "SecondViewController.h" 

@implementation AppDelegate 

@synthesize window = _window; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    viewController.title = @"First"; 
    navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
    self.window.rootViewController = navController; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    ... 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    ... 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    ... 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    ... 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    ... 
} 

- (void) showSecondViewController 
{ 
    secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    secondViewController.title = @"Second"; 
    [navController pushViewController:secondViewController animated:YES]; 
} 

@end 

實現,但是,當我在ViewController.m

- (IBAction)goSecondClicked:(id)sender 
{ 
    [[[UIApplication sharedApplication] delegate] showSecondViewController]; 
} 
發送消息給該方法

我收到以下編譯器錯誤:

自動引用計數問題 對於選擇 'showSecondViewController'

任何線索沒有已知的實例方法?

回答

5

你需要轉換,你得到的委託對象:

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 

然後調用appDelegate

3

方法您goSecondClicked操作方法改成這樣:

- (IBAction)goSecondClicked:(id)sender 
{ 
    [[[UIApplication sharedApplication] delegate] performSelector:@selector(showSecondViewController)]; 
} 

編輯:雖然這種替代方法適用於給定的情況,但應該注意的是,如果您更改方法名稱,編譯器將不會幫助您在您的委託中,忘記更改選擇器調用的名稱。所以,這應該謹慎使用。

+0

我真的希望那downvoters總是解釋他們的理由...雖然不是最好的解決方案,但我的答案很有效 – 2012-01-07 17:46:38

1

你也可以在你的AppDelegate.h

#define APP_DELEGATE (AppDelegate *)[[UIApplication sharedApplication] delegate] 

定義該宏在此之後,你可以調用你的選擇:

[APP_DELEGATE showSecondViewController]; 
相關問題