0

I can't seem to get a simple button placed in the upper right side of the iPad in the toolbar. I have the code in the viewDidLoad method.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.navigationItem.leftBarButtonItem = self.editButtonItem;

/*
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
 */

UIImage *image = [UIImage imageNamed:@"camera_30_30.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];

button.frame = CGRectMake( 0, 0, image.size.width, image.size.height);
button.showsTouchWhenHighlighted = YES;

[button addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];
//UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:self action:@selector(insertNewObject:)];

NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:barButtonItem];
//self.toolbarItems = items;
self.navigationItem.rightBarButtonItem = barButtonItem;
}
brianhevans
  • 1,183
  • 4
  • 15
  • 28

1 Answers1

0

Almost for sure your image is too large - a too big image will result in a white empty box. Also, your code has a bug in it (and superfluous information we didn't need to slog through:

- (void)viewDidLoad
{
[super viewDidLoad];

UIImage *image = [UIImage imageNamed:@"camera_30_30.png"];
...

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:self action:@selector(myAction)];

NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:barButtonItem];
self.toolbarItems = items; // Better be a toolbar.items = self.toolbarItems;

[items release];
[barButtonItem release]; // why I use ARC - hard to always get this right
}

I just verified a 100x200 or so image did not show, but a 30x30 did.

David H
  • 40,852
  • 12
  • 92
  • 138
  • Hi David H, I releaseed the barButtonItem and I made sure my image had a transparent background and is of size 30 x 30 and it still does not work. I am trying to place this button in a view controller that is navigated too after a couple of other screen in a Split View iPad application. Can you think of anything else I might be missing? – brianhevans Sep 05 '12 at 22:32
  • You should not be trying to set a button on another view controller - make a property on that view controller - could be an image, could be a button - then let THAT controller setup its toolbar in its viewDidLoad. If that isn't working you have to add some log messages, print out the toolbar items, the toolbar item itself, ask it for its image, ask the image for its size. you will find something wrong somewhere. – David H Sep 06 '12 at 01:00
  • Yea, I am pretty confused at this point :-( I created a new master-detail application with the updated code above and it works as I expected BUT when I place this same code in my ProductQualityViewController I don't get the same results. Nothing happens. @David H, how do you allow a view controller setup its own toolbar? I thought that is what I was assigning in my viewDidLoad. I appreciate your help. – brianhevans Sep 06 '12 at 18:02
  • I am using ARC in my newest master-detail sample test application where I was able to see this working so that is why I removed the release statements. – brianhevans Sep 06 '12 at 18:05
  • I added a blank one in the nib (nothing in it) then added the button in the viewDidLoad as you can see. Do you see the toolbar? If not you didn't add it to self.view. – David H Sep 06 '12 at 18:07
  • I did get it working in a master-detail app without issue. I can't get it working in the split view. Any idea what I need to do to get it working? – brianhevans Sep 06 '12 at 19:27
  • There's not magic here. These are all standard controls. I'm not using splitviews but if you can see the toolbar in a view, and you can get a reference to it, you can set the bar button on it with an action method pointing to any object you want. – David H Sep 06 '12 at 19:44
  • Ok, that's great news! What I am trying to figure out David is HOW to do that :-) I can see the toolbar in question. So it sounds like I need to find the name of the toolbar and add an action method to add this button, correct? So, how do I get the name of that toolbar is self.navigationItem isn't it? The action I am using is what you see above in my viewDidLoad. This is the ONLY way I know how to do it. I learn from example and this is the only example I have found. Your thoughts please? – brianhevans Sep 06 '12 at 20:03
  • navviationItems go into navigation bars. Toolbars are different. If you want it in a toolbar you need an IBOutlet to that toolbar, and if you want objects outside the class that owns the toolbar to be able to set barButtons in that toolbar, then you need to make it a property. In all cases of toolbars and navigationBars, the viewController has to have gotten as far as viewDidLoad BEFORE you can set these, as those outlets are nil until that point. – David H Sep 06 '12 at 20:34
  • Ok, the picture is getting less fuzzy David. I am trying to place this button in the navigation tool bar. I see this view controller when I push a button to bring up a new view controller. That view controller has a button on the left side to take me back to the previous screen. I guess this is a navigation controller I am looking at so how should I modify my approach? I am using nib files, not storyboards. – brianhevans Sep 06 '12 at 22:18