-1

II wanted to add a UIBarButton to my UINavigationController. I did that with the help of following code,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *addInfoButton = [[UIBarButtonItem alloc] initWithTitle:@"Add Info" style:UIBarButtonItemStylePlain target:self action:@selector(addCustomerInfo)];
    self.navigationItem.rightBarButtonItem = addInfoButton;
}

-(void) addCustomerInfo
{
    AddInfoViewController *addVC = [[AddInfoViewController alloc] initWithNibName:@"AddInfoViewController" bundle:nil];

    [addVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];

    [self presentModalViewController:addVC animated:YES];
}

Should I declare "-(void) addCustomerInfo" in .h file? I already tried that but no luck.

The code still throws the exeption,

2012-08-06 04:16:22.200 TableView[5698:f803] -[RootViewController addCustomerInfo]: unrecognized selector sent to instance 0x6c662b0 2012-08-06 04:16:22.202 TableView[5698:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController addCustomerInfo]: unrecognized selector sent to instance 0x6c662b0'

Cezar
  • 55,636
  • 19
  • 86
  • 87
Sibin
  • 133
  • 2
  • 8
  • You have a typo in your AddCustomerInfo method. Right now it is `-(void) aadCustomerInfo`. I don't know if your code is like that or it was an error you made when pasting it here, but you might wanna check that – Cezar Aug 05 '12 at 19:56
  • @cezarcp Thanks mate. Sorry for wasting your time on this stupid mistake. – Sibin Aug 05 '12 at 20:51

2 Answers2

1

Check out this tutorial: http://www.innovatelabs.in/2010/03/implementing-uibarbuttonitem/

I'm not so fluent with iOS, but usually when you use a selector, your add : to it @selector(addCustomInfo:)

and then the function would look like this:

-(void) addCustomInfo:(UIBarButtonItem *)myButton {
    NSLog(@"YOU CLICKED ME!");
}
La bla bla
  • 8,558
  • 13
  • 60
  • 109
  • I tried that function but it crashed again. -(void) aadCustomerInfo:(UIBarButtonItem *) addInfoButton – Sibin Aug 05 '12 at 13:17
  • I added the missing @ before the text. sorry, I write mostly for windows... Also, if it still crashes, can you post the error it writes to the console? Cause I've done something like that not too long ago using that code and it worked.. – La bla bla Aug 05 '12 at 14:00
  • Here is the error -[RootViewController addCustomerInfo:]: unrecognized selector sent to instance 0x6e625b0 2012-08-06 00:31:37.173 TableView[5197:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController addCustomerInfo:]: unrecognized selector sent to instance 0x6e625b0' – Sibin Aug 05 '12 at 14:40
  • @Sibin your log says that the method's name is `addCustomerInfo:` and not `addCustomInfo` as the code in your question. the method definition should match the selector passed when initializing the bar button. – Moxy Aug 05 '12 at 15:39
  • @Moxy I tried it still no luck. I posted my code above. Please have a look. – Sibin Aug 05 '12 at 18:57
1

The code for your class is correct. You need to change your App Delegate

I suggest creating a property in your App Delegate to store your navigation controller -@property (strong, nonatomic) UINavigationController *navController; - Don't forget to synthesize it. Then, when you create your Navigation controller, set it to your property - self.navController = [[UINavigationController alloc] init];

This will guarantee your NavigationController is properly retained and that it can be accessed correctly by other classes in your application.

Below some sample code that might make it clearer:

First the AppDelegate header file:

//AppDelegate.h
#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UINavigationController *navController;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navController;    
@end

And the implementation file:

//  AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.navController = [[UINavigationController alloc] init];    
    UIViewController *myVC= [[myVC alloc]     init];
    [self.navController pushViewController:myVC animated:NO];
    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    return YES;
}
Cezar
  • 55,636
  • 19
  • 86
  • 87
  • The code still give same exception and crashes. "Then, when you create your Navigation controller, set it to your property - self.navController = [[UINavigationController alloc] init]; " I did not understand it clearly. I put this code with " UIBarButtonItem *addInfoButton = [[UIBarButtonItem alloc] initWithTitle:@"Add Info" style:UIBarButtonItemStylePlain target:self action:@selector(addCustomerInfo)]; self.navigationItem.rightBarButtonItem = addInfoButton; " Please help – Sibin Aug 05 '12 at 15:16
  • No, the code should go in your App Delegate. Go to your AppDelegate.m file and locate where it is that your UINavigationController get's created. A common place for this is in your applicationDidFinishLaunching method. If that is really the case, create the property in your AppDelegate.h file and synthesize it in the AppDelegate.m Then, in the AppDelegate.m assign the Navigation Controller to the created property. I believe that should do the job. – Cezar Aug 05 '12 at 15:40
  • I've added some sample code to Illustrate what I am saying. Maybe it will make things clearer for you. – Cezar Aug 05 '12 at 16:10
  • It is not much different from my code except UIViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootController]; Please have look at modified code above. – Sibin Aug 05 '12 at 18:59
  • I commented on your question because there is a typo in the code you pasted there. Also, did you declare the navigation controller property using the keyword `strong`? Refer to this question to see why it's important http://stackoverflow.com/questions/6613186/what-does-the-strong-keyword-do – Cezar Aug 05 '12 at 19:59
  • Type was the issue. Thanks mate. Sorry for wasting your time on stupid mistake. – Sibin Aug 05 '12 at 20:49