0

I am iOS newbie. My app has multiple view controllers, so I abstracted the code as

NavigationBar.h

@interface NavigationBar : NSObject
- (void) title: (NSString *) title withViewController: (UIViewController *) viewController;
@end

NavigationBar.m

#import "NavigationBar.h"
#import "PennyNavigationController.h"


@implementation NavigationBar
- (void)title:(NSString *)title withViewController:(UIViewController *)viewController {
    viewController.title = title;
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu"
                                                                                       style:UIBarButtonItemStyleBordered
                                                                                      target:(PennyNavigationController *) viewController.navigationController
                                                                                      action:@selector(showMenu)];
    viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[self getImageNamed:@"Add" withDimension:30] style:UIBarButtonItemStylePlain target:self action:@selector(sayHello:)];

}

- (IBAction)sayHello:(id)sender
{
    NSLog(@"add new transaction");
}

- (void)btnClicked:(id)sender {
//    NSLog(@"add new transaction");
}

When the app starts, and I click on Add image, I don't see the log, additionally I see that the app closes.

What am I doing wrong?

P.S: Seems I am doing exactly mentioned in Add a custom selector to a UIBarButtonItem, but still it doesn't work

Community
  • 1
  • 1
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • It's not clear why you're subclassing UINavigationController, and putting this code in there. The navigationItem belongs to the UIViewController, so the code for any bar button items is normally in there, not in a navigation controller subclass. – rdelmar Aug 31 '14 at 23:05
  • apologies, I did not paste the code corrrectly, please see now and advice – daydreamer Aug 31 '14 at 23:08
  • If the app closes then it is probably crashing. You should get an exception message to tell you why. How are you invoking the `title:withViewControler` method? – Paulw11 Aug 31 '14 at 23:12
  • I still don't understand what you're trying to do. The navigation bar shouldn't be the one implementing button actions. Those actions, and the code for creating the buttons belong with the individual view controllers. – rdelmar Aug 31 '14 at 23:15
  • but what if `Add` button is agnostic to view Controllers? and available on `Navigation Bar`? – daydreamer Aug 31 '14 at 23:23
  • This is definitely unorthodox... I'm with everyone else when I say that subclassing the navigation bar/controller doesn't seem like the best option here, and the references to view controller/navigation controller and the casting here makes it really hard to figure out what you're trying to do. – Mike Aug 31 '14 at 23:27
  • Conventionally, the view controller is embedded in a navigation controller, and each view controller decides what navigation items it has and handles what happens when those buttons are pressed... – Mike Aug 31 '14 at 23:27

0 Answers0