Getting Memory allocation problem at UIBarButtonItem in Iphone sdk - objective-c

Here I am getting memory allocation problem at UIBarButtonItem and the related code for that is:
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackOpaque;
[toolbar setFrame:CGRectMake(0, 350,320,20)];
[self.view addSubview:toolbar];
UIBarButtonItem* barItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(categoryConfig:)] ;
rightBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:#selector(dialogOtherAction:)] ;
UIBarButtonItem* barItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(categoryConfig:)] ;
NSArray *items = [NSArray arrayWithObjects: barItem1,rightBarItem,barItem2, nil];
[barItem1 release];
[barItem2 release];
[rightBarItem release];
[toolbar setItems:items animated:NO];
after adding UIBarButtonItems into the array items I released them.even though its showing allocations at barbuttons.
can any help me for this?
Thank you,
Monish.

You allocated toolbar with toolbar = [UIToolbar new];, so you need to release it.

Related

Centering on a UIToolBar with a UIToolbarButton on either side

I am trying to center some text on a UIToolBar with a button on either side, but I am having some problems.
I have the following code:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:toolbarInitialFrame];
toolBar.barStyle = UIBarStyleBlackTranslucent;
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 25)];
[label setText:#"Title"];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont boldSystemFontOfSize:16]];
UIBarButtonItem * labelEmu=[[UIBarButtonItem alloc] initWithCustomView:label];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelAlarmAndDismissDatePicker:)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(setAlarmAndDismissDatePicker:)];
So what I want is the cancel button (cancelButton) on the left, the title (labelEmu) centered in the middle, and the done button (doneButton) on the right. Something like this:
|[Cancel] Title [Done]|
I was hoping I could achieve that with this:
[toolBar setItems:[NSArray arrayWithObjects: cancelButton, spacer, labelEmu, spacer, doneButton, nil]];
But unfortunately what I get is this:
|[Cancel]Title [Done]|
After some playing about, Ive found that this line:
[toolBar setItems:[NSArray arrayWithObjects: cancelButton, spacer, labelEmu, spacer, nil]];
...produces this:
|[Cancel] Title |
Where the title is centered, ignoring the fact that the cancel button is there. But when I do this:
[toolBar setItems:[NSArray arrayWithObjects: spacer, labelEmu, spacer, doneButton, nil]];
...it produces this:
| Title [Done]|
Where the title is centered in the space between the far left edge and the right side of the done button. It is not ignoring the done button in the same way it ignores the cancel button.
Can anybody point me in the right direction to get what I am after? Sorry about all the naff ASCII diagrams! :)
Maybe try using:
[[UIBarButtonItem alloc] initWithTitle:#"yourtitle" style:UIBarButtonItemStylePlain target:nil action:nil];
It might help if the UITollbar will be in charge on the label.
EDIT
Try adding:
[label sizeToFit];
Before adding it to the tollBar
---EDITED---
After further investigation - turns out problem really was label. You have set it's width to 200.
Should be a lot less for it to autoresize correctly.
See this code:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 200, 320, 50)];
toolBar.barStyle = UIBarStyleBlackTranslucent;
[self addSubview:toolBar];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 25)];
[label setText:#"Title"];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[label sizeToFit];
UIBarButtonItem * labelEmu=[[UIBarButtonItem alloc] initWithCustomView:label];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelAlarmAndDismissDatePicker:)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(setAlarmAndDismissDatePicker:)];
[toolBar setItems:[NSArray arrayWithObjects: cancelButton, spacer, labelEmu, spacer, doneButton, nil]];
It generated something like this:

how do you add a right button to a uinavigationcontrol

How can I add a button to a UINavigationControl that I added to a ViewController? (The project isn't UINavigationControler based.)
Here's the code I've added to my viewDidLoad method:
navigation = [[UINavigationController alloc] init];
navigation.view.frame = CGRectMake(0, 0, 1024, 748);
navigation.navigationBar.tintColor = [UIColor blackColor];
navigation.navigationBar.alpha = 1.0f;
navigation.navigationBar.translucent = NO;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
[navigation.navigationItem setRightBarButtonItem:cancelButton animated:YES];
[cancelButton release];
[self.view addSubview:navigation.view];
[self pushPresentationList];
Can someone help me fix lines 7-9 please?
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Button"
style:UIBarButtonSystemItemDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Awesome"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[navigationBar pushNavigationItem:item animated:NO];
Do you need an UINavigationController? Why not just use a simple UINavigationBar. Here is some sample code:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:
CGRectMake(0,0,self.view.frame.size.width, 49.0f)];
[self.view addSubview:navBar];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
UINavigationItem *rightBarButtonItem = [[UINavigationItem alloc] init];
[rightBarButtonItem setRightBarButtonItem:cancelButton];
[navBar pushNavigationItem:rightBarButtonItem animated:NO];

How to mark the UIToolBar autoresze when it rotated

I have created a toolbar. Just like this:
UIToolbar *toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, toolBarHeight)];
toolbar.autoresizingMask=UIViewAutoresizingFlexibleWidth;
UIBarButtonItem *itemNull=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
UIBarButtonItem *item0=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item1=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item2=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item3=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:#selector(pre)]
autorelease];
UIBarButtonItem *item4=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(pre)] autorelease];
itemNull.width=frame.size.width-itemwidth*5;
item0.width=itemwidth;
item1.width=itemwidth;
item3.width=itemwidth;
item4.width=itemwidth;
item2.width=itemwidth;
[toolbar setItems:[NSArray arrayWithObjects: itemNull,item0,item1,item2,item3,item4, nil]];
[self addSubview:toolbar];
[toolbar release];
It is added to a view. But the items are out of the sign when I rotated the view.
and I want to know how to make it visible and not to reset it's frame.
Did the items have the Property like autoresize?
there is an inbuilt hidden toolbar..
[self.navigationController setToolbarHidden:NO animated:YES]; // i hope its a navigation based application.
modify code
UIBarButtonItem *itemNull=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
UIBarButtonItem *item0=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item1=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item2=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(pre)] autorelease];
UIBarButtonItem *item3=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:#selector(pre)]
autorelease];
UIBarButtonItem *item4=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(pre)] autorelease];
[self.navigationController setToolbarHidden:NO animated:YES];
self.toolbarItems = [NSArray arrayWithObjects: itemNull, item0, item1, item2, item3, item4, nil];
hope it helps. happy coding :)

UIActionSheet on iPad frame too small

I am trying to display a UIActionSheet on the iPad with a UIPickerView in it, I have the equivalent code working for iPhone so my UIPickerView delegate and datasource stuff work, but on the iPad when I use -[UIActionSheet showFromRect:inView:animated:] the result UIPopover/UIActionSheet is way too small and I can not seem to set the frame size, also none of the buttons are displayed.
I don't know if this is because they are outside the bounds or there is something else going on. This is what my code looks like after I have removed all non-essential code (iPhone etc). Does anybody know what I am doing wrong, does anybody know of any examples.
CGRect thePickerFrame = CGRectMake(0, 0, 320.0, 485.0);
UIPickerView * thePickerView = [[UIPickerView alloc] initWithFrame:thePickerFrame];
[pickerActionSheet release], pickerActionSheet =
[[UIActionSheet alloc] initWithTitle:#"Choose" delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Next", nil];
thePickerView.showsSelectionIndicator = YES;
thePickerView.dataSource = self;
thePickerView.delegate = self;
[pickerActionSheet addSubview:thePickerView];
[thePickerView selectRow:0 inComponent:0 animated:NO];
[thePickerView release];
[pickerActionSheet showFromRect:currentTextField.bounds
inView:currentTextField animated:NO];
pickerActionSheet.frame = thePickerFrame;
I think that UIActionSheet is not resizable, try to comment in your code the line with [pickerActionSheet addSubview:thePickerView]; and you will see that the ActionSheet fits perfecly to the buttons.
I would recommend a UIPopoverController with a custom UIViewController. Something like this:
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"back" style:UIBarButtonItemStyleBordered target:self action:#selector(BACK_ACTION:)];
UIBarButtonItem *chooseButton = [[UIBarButtonItem alloc] initWithTitle:#"Choose" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(NEXT_ACTION:)];
UIBarButtonItem *fixed1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fixed2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:cancelButton, fixed1, chooseButton, fixed2, nextButton, nil]];
UIPickerView *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
CGRect thePickerFrame = thePickerView.frame;
thePickerFrame.origin.y = toolbar.frame.size.height;
[thePickerView setFrame:thePickerFrame];
UIView *view = [[UIView alloc] init];
[view addSubview:thePickerView];
[view addSubview:toolbar];
UIViewController *vc = [[UIViewController alloc] init];
[vc setView:view];
[vc setContentSizeForViewInPopover:CGSizeMake(320, 260)];
popover = [[UIPopoverController alloc] initWithContentViewController:vc];
thePickerView.showsSelectionIndicator = YES;
thePickerView.dataSource = self;
thePickerView.delegate = self;
[thePickerView selectRow:0 inComponent:0 animated:NO];
[popover presentPopoverFromRect:currentTextField.bounds inView:currentTextField permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Where popover is a class variable declared in .h (UIPopoverController *popover;).
By the way, I'm using ARC, so there is no release in the code.
I managed to make it work using a silly way.
For your reference:
Setup PickerView.
UIActionSheet's width can't be adjusted somehow, so have to adjust pickerView accordingly. Height wise, you can adjust with the amount of "\n" in actionSheet title.
UIDatePicker * pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 270, 220)];
pickerView.datePickerMode = UIDatePickerModeDate;
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:#"\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet addSubview:pickerView];
[actionSheet showFromRect:button.frame inView:button.superview animated:YES];
[actionSheet release];
Set Frame or Bound doesn't work for me.
[actionSheet setBounds:pickerView.frame];
[actionSheet setFrame:pickerView.frame];
Agreed on using UIPopoverController, but don't know why, it got a serious UI delay when I put UIDatePickerView into popover (it took almost 1 sec lag to pop up) which I can't find the root cause. So have to fallback to above method.
Happy Coding.

I am not able to add UIBarButtonItem in UINavigationController

navigator=[[UINavigationController alloc]initWithRootViewController:contacts];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]
initWithTitle:#"Delete"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(makeCall)];
[[self.navigator navigationItem] setLeftBarButtonItem:nextButton];
[nextButton release];
I am not able add UIBarButtonItem in UINavigationController ,please help
Speaking off the cuff here, perhaps instead of
[[self.navigator navigationItem] setLeftBarButtonItem:nextButton];
use
self.navigator.navigationBar.items = [NSArray arrayWithObject:nextButton];
to do what you want.