Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No known instance method for selector

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?

like image 345
VansFannel Avatar asked Jan 17 '23 22:01

VansFannel


1 Answers

You will need to cast the delegate object that you get as:

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

Then call the method on appDelegate

like image 128
Hetal Vora Avatar answered Jan 29 '23 11:01

Hetal Vora