Unknown instance method for selector

I am developing an iOS 4 application using the latest SDK, Xcode 4.2 and ARC .

I added the appDelegate.h method

 #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 is 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 this method in ViewController.m

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

I get the following compiler error:

Automatic reference counting error There is no known instance method for selector 'showSecondViewController'

Any clue?

+4
source share
3 answers

You will need to specify the delegate object, which you will get as:

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

Then call the method on appDelegate

+5
source

Change the goSecondClicked mode of action to this:

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

EDIT: although this alternative works for this situation, it should be noted that the compiler will not help you if you change the method name in your delegate and remember to change the name when calling the selector. Therefore, this should be used carefully.

+3
source

You can also define this macro on your AppDelegate.h

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

After that, you can call your selector with:

 [APP_DELEGATE showSecondViewController]; 
+1
source

Source: https://habr.com/ru/post/1388504/


All Articles