0

I wanted to create a barbutton that was a custom image so I created a subclass UIBarButtonItem.

    @implementation UIBar

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{

    UIImage *menuButtonImage = [UIImage imageNamed:@"Camera.jpg"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:menuButtonImage forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, menuButtonImage.size.width, menuButtonImage.size.height);
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self action:@selector(takePicture) forControlEvents:UIControlEventTouchUpInside];


  //  self->navigationItem.rightBarButtonItem = menuButton;

    [UIViewController release];
}

But the error I'm getting on the line:

 self.navigationItem.rightBarButtonItem = button;

incomplete definition of type struct objc_class

Does this error have anything to do with my class not being initialised properly? I'm new to Objective-C.

Thanks for your help.

Andrea F
  • 733
  • 2
  • 9
  • 16

2 Answers2

0

In your category method, you are not returning any UIBarButtonItem instance. Try to return the menuButton.

And I am not sure why you need to call class method of UIViewController. The method is for instance, not a class method. Remove that line too.

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{

    UIImage *menuButtonImage = [UIImage imageNamed:@"Camera.jpg"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:menuButtonImage forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, menuButtonImage.size.width, menuButtonImage.size.height);
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self action:@selector(takePicture) forControlEvents:UIControlEventTouchUpInside];
    return menuButton; 
}
Valent Richie
  • 5,226
  • 1
  • 20
  • 21
0

If you want custom bar button then you can use the following code in your viewDidLoad method of viewController.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action: @selector(takePicture)forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;

If you want to subclass UIBarButtonItem then you can do the following.

@implementation UIBar

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    return menuButton;
}

and in your viewController viewDidLoad method do the following

 UIBar *customBarItem = [UIBar barItemWithImage:buttonImage target:self action:@selector(takePicture)];
 self.navigationItem.leftBarButtonItem = customBarItem;

Both the method will work.

Prasad Devadiga
  • 2,573
  • 20
  • 43
  • For the method in ViewDidLoad- UIBar *customBarItem = [UIBar barItemWithImage:menuButtonImage target:self action:@selector(takePicture)]; Does the Image need to be declared in ViewController.h? It's telling me it doesn't know what it is. Sorry about all the questions. You have all been very helpful! – Andrea F Jun 07 '13 at 07:21
  • @AndreaF Yes it needs to be declared in viewController, `menuButtonImage` is a `UIImage` object – Prasad Devadiga Jun 11 '13 at 03:37
  • hmm It's still not showing up. Maybe I'm doing something wrong in XIB? I just dragged a toolbar on. – Andrea F Jun 11 '13 at 07:02
  • Is it not a UINavigationController ? – Prasad Devadiga Jun 11 '13 at 08:57
  • It's a ViewController. Should it be a navigation controller? – Andrea F Jun 11 '13 at 09:57
  • so you want to add custom bar button to toolbar, am I right?? What I suggest is if you have navigation behaviour in your viewController then you should go for navigationController otherwise toolbar is also fine. – Prasad Devadiga Jun 11 '13 at 10:15
  • If you want to add barbutton to toolbar then you should make use of `items` property of `UIToolBar`. This link might help you http://stackoverflow.com/questions/7711999/how-to-add-a-uitoolbar-programmatically-to-an-ios-app – Prasad Devadiga Jun 11 '13 at 10:17