Why is my UInavigationController right button disappearing after it is loaded - objective-c

I'm using the following code to push a view controller when user click on a UIButton
- (IBAction)showListPicker:(id)sender {
if([audioPlayer isPlaying])
{
[audioPlayer stop];
}
ListPicker *lp = [[ListPicker alloc] initWithStyle:UITableViewStyleGrouped];
[[self navigationController] pushViewController:lp animated:YES];
[lp release];
}
In the ViewDidLoad of ListPicker I use the following code to add right navigational control button
-(void)viewDidLoad{
[[self navigationController] setNavigationBarHidden:NO];
[[[self navigationController] navigationBar] setTintColor:[UIColor brownColor]];
[[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(doSomething:)];
[[self navigationItem] setRightBarButtonItem:button];
[button release];
}
When the view is loaded, I can see the right button briefly but then it disappear right away. What am I doing wrong?
Thank you.

My guess is that your ListPicker's XIB contains another navigation bar, obstructing the actual navigation controller's bar. This is why you see it for an instant and then it "disappears". If it's not something in the XIB, check the code all the way from instantiating that new view controller until the viewDidAppear of ListPicker.

Related

How to change the title of the UINavigationBar back button to "Back"

When I push a view onto the navigation controller the back button's title get's set to the title of the previous view. How can I get the back button to just say "Back"?
Write this code in your viewwillappear:
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = _backButton;
[_backButton release];
_backButton = nil;
In the previous view controller, have it set its title in viewWillAppear, and then in the code that pushes the new view controller, have it change its title to 'Back.'
Example:
-(void)showNextScreen{
[self setTitle:#"Back"];
[self.navigationController pushViewController:asdf animated:YES];
}
-(void)viewWillAppear{
[super viewWillAppear];
[self setTitle:#"My Actual Title"];
}

Creating a common UIToolbar helper file and methods for Objective C

I have build a view with a UIToolbar which is working great.
This toolbar will be appearing right across the app, and right now I am copy/pasting the code into lots of different files.
I do not want to repeat myself, and am looking to create a helper file that will include the toolbar setup and the methods linked to the toolbar in every file I need.
I've tried putting the following code into a .h .m file and inheriting from UIView, but there is a problem because there is a reference to self.navigiationItem
Is there a way that I can create a common Objective C file that will have all the code and methods I want to use?
Thanks.
- (void)viewDidLoad
// ...
// appears in viewDidLoad
// ---- TOOLBAR -----------//
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100.0, 44.01f)];
//[toolbar setBackgroundColor:[UIColor blackColor]];
//[toolbar setTintColor:[UIColor redColor]];
//[toolbar.layer setBorderColor:[[UIColor redColor] CGColor]];
// Bar buttons
UIBarButtonItem *barReloadBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(btnReload:)];
[barReloadBtn setStyle:UIBarButtonItemStyleBordered];
// Profile bar button
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"111-user" ofType:#"png"]];
UIBarButtonItem *barProfileBtn = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:self action:#selector(btnProfile:)];
// Button array
NSMutableArray *buttons = [[NSMutableArray alloc] init];
[buttons addObject:barProfileBtn];
[buttons addObject:barReloadBtn];
[toolbar setItems:buttons];
// Set nav items
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
// memory cleanup
[image release];
[buttons release];
[barReloadBtn release];
[barProfileBtn release];
[toolbar release];
// ---- /TOOLBAR -----------//
}
#pragma mark - IBActions
-(IBAction) btnProfile:(id)sender
{
UserProfileVC *userProfileVC = [[UserProfileVC alloc] initWithNibName:#"UserProfileVC" bundle:[NSBundle mainBundle]];
UINavigationController *tmpNavCon = [[UINavigationController alloc] initWithRootViewController:userProfileVC];
[self.navigationController presentModalViewController:tmpNavCon animated:YES];
[tmpNavCon release];
[userProfileVC release];
}
-(IBAction) btnReload:(id)sender
{
NSLog(#"Not done yet");
}
navigationItem is a property of UIViewController, not UIView. If you've got common functionality like this, I would inherit from UIViewController, add your custom logic to viewDidLoad (or wherever is appropriate) and then inherit your view controllers from that class. Just make sure you call [super viewDidLoad] from your subclasses' implementations of viewDidLoad.

UIToolbar Memory Leak

Currently I have a navigation based application and obviously the RootViewController is a UITableView. However, I deemed it necessary to create a UIToolbar that floats above the UITableView. Currently I do this like this.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create a button
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(account_details)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
[infoButton release];
[self.tableView reloadData];
}
However, after using the Leaks instrument tool, I was able to determine that this was the cause for a few memory leaks, only small, but memory leaks nonetheless. I then drilled down even further and was able to pin point the exact lines that are causing the memory leaks. They are the following.
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(account_details)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
[self.navigationController.view addSubview:toolbar];
I am struggling to figure out how to remove these memory leaks and thus causing my application to run smoother. Any help would be appreciated as to why the above lines are causing leaks!
A new toolbar is created every time the view appears, added to the view and never released. This means that both that tool bar and its bar button item will last forever. You can fix this by simply releasing the toolbar after you add it to the view, or sending it the autorelease message when you create it. So, a decent way to do this would be to replace:
toolbar = [[UIToolbar alloc] init];
with:
toolbar = [[[UIToolbar alloc] init] autorelease];
Also, the way you're doing this, every time your view appears you end up adding another toolbar to the navigation controller's view. So you almost certainly have quite a few of these objects sitting on top of each other (so you will still see leaks until the navigation view finally goes away). What you might want to do is keep this toolbar as an ivar. When your view disappears, remove the toolbar from the nav controller's view. When it appears, add it. Create the toolbar itself in your viewDidLoad method and clean it up in viewDidUnload then release it in dealloc. So your new class might look like this (let's assume you create a synthesized property named toolbar that's retain):
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* toolbar = [[[UIToolbar alloc] init] autorelease];
// set up toolbar
[self setToolbar:toolbar];
// other load code
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[[self navigationController] view] addSubview:[self toolbar]];
// other vwa code
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[self toolbar] removeFromSuperview];
}
- (void)viewDidUnload
{
[self setToolbar:nil];
[super viewDidUnload];
}
- (void)dealloc
{
UIToolbar* toolbar = [self toolbar];
[toolbar removeFromSuperview]; // shouldn't ever need this, but be safe
[toolbar release];
// other dealloc
[super dealloc];
}

Buttons on top of UIPopoverController

I want to add two Buttons on top of UIPopoverController like it is shown in following screenshots:
HTML Edit
Thanks for helping me!
Add your view controller to a UINavigationController, then add the Navigation Controller to the UIPopoverController. Then in your UIViewController's viewDidLoad method, put this code in:
UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:#"Ok" style:UIBarButtonItemStyleBordered target:self action:#selector(okayButtonPressed)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelButtonPressed)];
self.navigationItem.title = #"My Title";
[self.navigationItem setLeftBarButtonItem:cancelButton animated:NO];
[self.navigationItem setRightBarButtonItem:okButton animated:NO];
[cancelButton release];
[okButton release];
You need to initialize your popover with a UINavigationController directly. Then set the root view to your custom view controller.
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
Use a UINavigationController as the pop-over. Then, access the .navigationBar property of the navigation controller, get the .topItem, and set its .leftBarButtonItem and .rightBarButtonItem.
I wouldnt use a navigationcontroller like the previous posters suggested, apple recommends not using navigationcontrollers on ipad (with good reason) it doesnt behave as youd expect when pushing VC into the stack when used in popovers, now you dont really want to use the "navigation" aspect of it, but i wouldnt use navigationcontroller just because uw ant the bar....Use a UIToolBar instead, and set its buttons to whatever you want...no need to use a navigation controller here...
When I do this my navBar doesn't seem to fit properly inside of the UIPopoverController, as shown in the below:
http://www.flickr.com/photos/coleorton/4752223066/
Here's what I'm doing:
// alloc the Direct Reports view controller.
ToolsViewController *toolsViewController = [[[ToolsViewController alloc] init] autorelease];
UINavigationController *toolsNavController = [[[UINavigationController alloc] initWithRootViewController:toolsViewController] autorelease];
toolsNavController.title = #"Tools";
toolsNavController.view.frame = CGRectMake(0.0, -10.0, 320.0, POPOVER_HEIGHT);
if(![self.toolsPopoverController isPopoverVisible]){
// show popover
self.toolsPopoverController = [[[UIPopoverController alloc] initWithContentViewController:toolsNavController] autorelease];
self.toolsPopoverController.delegate = self;
self.toolsPopoverController.popoverContentSize = CGSizeMake(320.0, POPOVER_HEIGHT);
[self.toolsPopoverController presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
} else {
// close popover
[self.toolsPopoverController dismissPopoverAnimated:YES];
}
This worked!
//Determine how to present this view based on device
if ([UIDevice currentDevice ].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
BNRAssetTypeViewController *contentViewController = [[BNRAssetTypeViewController alloc] init];
UINavigationController *popOverNavigation = [[UINavigationController alloc] initWithRootViewController:contentViewController];
self.assetPickerPopover = [[UIPopoverController alloc] initWithContentViewController:popOverNavigation];
[self.assetPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
then in the init function of contentViewController add this
//add a barbutton item which will help in adding new type
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addNew:)];
//set bar item to right side of navbarite
self.navigationItem.rightBarButtonItem =bbi ;

Adding UIBarButtonItem to UINav..Controller

i am not sure what i am missing here. I Have a custom UINavigationController and i am trying to add a persistant UIBarButtonItem to the bar.
-(void)viewDidLoad
{
self.navigationBar.barStyle = UIBarStyleBlack;
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:#"Nope..."
style:UIBarButtonItemStyleBordered
target:self
action:#selector(goBack:)];
self.navigationItem.leftBarButtonItem =bbi;
[bbi release];
}
-(void)goBack:(id)sender
{
NSLog(#"go back now");
}
what am i missing here? - BTW, i do not want to/ will not use IB.
UPDATE:
Currently this is the closest i can get:
-(void)viewDidLoad
{
self.navigationBar.barStyle = UIBarStyleBlack;
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
navBar.barStyle = UIBarStyleBlack;
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Currently Playing..."];
[navBar pushNavigationItem:navItem animated:NO];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleBordered target:self action:#selector(goBack:)];
navItem.rightBarButtonItem = editButton;
[self.view addSubview:navBar];
[editButton release];
[navItem release];
[navBar release];
[super viewDidLoad];
}
It's terrible i have to add an entire navbar to a UINavigationController that already has a navbar....if i try to use the existing, i get this error:
'NSInternalInconsistencyException', reason: 'Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'
....really???
navigationItem must not be set on the UINavigationController instance but on the view controller of the view which is displayed "inside" the navigation controller.
Setting self.navigationItem on your navigation controller would work if your controller was itself pushed into another navigation controller.