I'm developing an iOS 4 application using latest SDK, XCode 4.2 and ARC.
I've added a method to 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
And it's implemented in 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
But, when I send a message to that method in ViewController.m
- (IBAction)goSecondClicked:(id)sender
{
[[[UIApplication sharedApplication] delegate] showSecondViewController];
}
I get the following compiler error:
Automatic Reference Counting Issue No known instance method for selector 'showSecondViewController'
Any clue?
You will need to cast the delegate object that you get as:
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
Then call the method on appDelegate
Change your goSecondClicked action method to this:
- (IBAction)goSecondClicked:(id)sender
{
[[[UIApplication sharedApplication] delegate] performSelector:@selector(showSecondViewController)];
}
EDIT: although this alternative works for the given situation, it should be noted that the compiler won't help you if you change the method name in your delegate and forget to change the name on the selector call. So, this should be used carefully.
You can also define this macro on your AppDelegate.h
#define APP_DELEGATE (AppDelegate *)[[UIApplication sharedApplication] delegate]
After this, you can invoke your selector with:
[APP_DELEGATE showSecondViewController];
来源:https://stackoverflow.com/questions/8672246/no-known-instance-method-for-selector