views:

140

answers:

2

When we go portrait mode while using a UISplitViewController, they will provide us by a barButtonItem. How can I use a customView for that barButtonItem?

I tried the following way. It works if I start with portrait orientation. But if I go landscape and when I come back it crashes.

In viewDidLoad

UIImage *image = [UIImage imageNamed:@"home.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, 22.00, 22.00);    
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(showHomeDetails) forControlEvents:UIControlEventTouchDown];    
homeButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];

Then in willHideViewController

barButtonItem = homeButton;

What shud I do? OR is there an alternate solution?

A: 

I believe it should be this:

UIImage *image = [UIImage imageNamed:@"home.png"];
UIImageView *imageView = [UIImageView initWithImage:image];
homeButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
[homeButton addTarget:self action:@selector(showHomeDetails)];
[homeButton setBounds:CGRectMake(0, 0, 22.00, 22.00)];

Hope this solves your problem
jrtc27

jrtc27
I think this may also work. Thanks anyway for helping me.
wolverine
Hi: what would be the code for showHomeDetails?
Antonio
Sorry to reply so late. I just saw ur comment.
wolverine
Antonio, ask it as a separate question - you will get more replies.
jrtc27
A: 

Rewrote it like this inside the willHideViewController and its done.

UIImage *image = [UIImage imageNamed:@"news.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, image.size.width, image.size.height );    
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(showSideTable) forControlEvents:UIControlEventTouchUpInside];    
[barButtonItem setCustomView:button];

Main change is removed [button release].

wolverine
I'm a bit confused with the target and action you add to the button. Can you post the implementation of the showSideTable method?
Antonio
- (void) showSideTable{ [popover presentPopoverFromBarButtonItem:[toolBar2.items objectAtIndex:2] permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];}
wolverine