Kiran
Kiran

Reputation: 5526

ios ios5 exc_bad_access with navigation controller (basic)

I am creating a basic navigation based application and am seeing an exc_bad_access error. Can some one please point me what is wrong? I have just 2 screens and here is the code I am using:

in AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

  UINavigationController *navcon = [[UINavigationController alloc]init];
  psLaunchVC* pvc = [[psLaunchVC alloc]init];
  [navcon pushViewController:pvc animated:NO];

  [self.window addSubview:navcon.view];
    [self.window makeKeyAndVisible];
    return YES;
}

psLaunchVC comes up fine as the first screen. I am trying to launch psTipVC from psLaunchVC.

psLaunchVC has an action declared in .h as

-(IBAction)showTip:(id)sender;

and implemented in .m as

- (IBAction) showTip:(id)sender
{
  // psTipVC *pst = [[psTipVC alloc]init];

  psTipVC *pst = [[psTipVC alloc]initWithNibName:@"psTipVC" bundle:nil];

  [self.navigationController pushViewController:pst animated:YES];

}

The showTip is connected in IB as action for touchUpInside. However, when this code is executed, I see an exc_bad_access error. Can some one help me what is wrong here? is self.navigationController the right way to access the nav controller?

Declarations for reference:

@interface psLaunchVC : UIViewController 
@interface psTipVC : UIViewController

Actual error message:

2011-12-29 00:03:13.739 passport[633:707] -[__NSCFString showTip:]: 
     unrecognized selector sent to instance 0x18f5e0
2011-12-29 00:03:13.748 passport[633:707] 
          *** Terminating app due to uncaught exception 
          'NSInvalidArgumentException', 
          reason: '-[__NSCFString showTip:]: 
          unrecognized selector sent to instance 0x18f5e0'

Upvotes: 0

Views: 1182

Answers (1)

MyztikJenz
MyztikJenz

Reputation: 1646

The message you're getting looks like psLaunchVC has been released and the outlet is pointing to garbage. Are you perhaps compiling this with ARC enabled? If so, your navigation controller is most likely getting released out from underneath you when application: didFinishLaunchingWithOptions: returns as it's not retained by anyone. Try reworking that method to look like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

  psLaunchVC* pvc = [[psLaunchVC alloc]init];
  UINavigationController *navcon = [[UINavigationController alloc]initWithRootViewController:pvc];

  self.window.rootViewController = navcon;
  [self.window makeKeyAndVisible];
  return YES;
}

UIWindow really wants a rootViewController, not just the view to display. Also, initWithNibName:bundle: is the designated initializer for UIViewControllers, psLaunchVC should really be init'd with that instead.

Upvotes: 1

Related Questions