How to change the text of UINavigationBar back button? - objective-c

I want to ask a question about the iPhone application. I create the UINavigationController programmatically. And I use the UITableView to do the following thing. However, I don't know how to change the text of the text in the back button (see below, in this case is 'Plays') in code level? Thank you very much.
alt text http://www.freeimagehosting.net/image.php?02730817e4.png
Link: http://www.freeimagehosting.net/image.php?02730817e4.png

To customize the back button you modify the view controller you are going back to. So you can either set the title for your "Plays" view controller:
- (void)viewDidLoad {
// ...
[self setTitle:#"Whatever"];
}
Or access the back button item:
- (void)viewDidLoad {
// ...
// target/action must be nil
self.navigationItem.backBarButtonItem =
[[[UIBarButtonItem alloc] initWithTitle:#"Whatever"
style:UIBarButtonItemStyleBordered
target:nil action:nil] autorelease];
}

You have to actually change the text of the back button before pushing the new view controller onto the stack.Otherwise the back button text will not be displayed.

put this in you viewDidLoad, and i think you will get what you need.
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"yourTitle"
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];

Related

Storyboard: How to set a UIToolbar at the bottom of a UITableViewController controlled by a UINavigationViewController

I'm new to the Cocoa topic. I build with the Storyboard a small application which runs just fine.
I'm pushing UITableViewController to another one, controlled by the UINavigationViewController. Now I need a UIToolbar with a UIBarButtonItem fixed at the bottom of the screen.
Here can you see my Storyboard So I added via drag and drop a Toolbar and in the toolbar an button into the Period TableViewController, thus it is on the same hierarchic level like the UITableView.
Unfortunately neither the "PayOff" Button nor a bottom Toolbar does appear in the app.
Can you help me, what is wrong with my Storyboard?
In your ViewController try adding this
[self.navigationController setToolbarHidden:NO animated:YES];
and use this to add BarItems to it
[self setToolbarItems:#[item1, item2, item3] animated:YES];
I'm using this method to add a scan button to a UITableViewController:
UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
...
...
UIBarButtonItem *scanItem = [[UIBarButtonItem alloc] initWithCustomView:scanButton];
[self setToolbarItems:#[leftSpace, scanItem, rightSpace] animated:YES];
You should be careful because the ToolBar visibility is set for the entire NavigationController you are using and you should show/hide it when needed.
Also the items on it need to be set on each controller (I have this issue, maybe there is a better way to do it)
Hope this helps.

Replacing UITabBar with UIToolbar programmatically

I'm trying to write a tabbed application where each tab is a Navigation Controller. This tab bar appears at every view in the navigation controller as its being inferred on each view.
I would like to replace this tab bar on a detail view with a tool bar with a couple of buttons on it.
I've tried the following code in that detail view's viewDidLoad: method
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *accept = [[UIBarButtonItem alloc] initWithTitle:#"Accept"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
UIBarButtonItem *decline = [[UIBarButtonItem alloc] initWithTitle:#"Decline"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
NSArray *items = [NSArray arrayWithObjects:accept, decline, nil];
[self.navigationController.toolbar setItems:items animated:YES];
// code suggested
[self.view addSubview:self.navigationController.toolbar];
It still doesn't show up. Though hides the tab bar now for adding the following line in the view that's presenting the detail view:-
theDetailTableViewController.hidesBottomBarWhenPushed = YES;
Have I missed something?
I usually put toolbarHidden = YES or NO, as applicable, in the viewWillAppear or viewDidAppear methods. I am not sure if that is why it is not working for you, but you need to address when you return to the presenting view anyway.
If you don't address it, the toolbar will still be visible when you go back.
Wherever you are pushing your detailViewController from, do this to hide the Tab Bar in the detail view:
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController detailViewController animated:YES];
and in your detail view, just add the ToolBar as a subview to the detailView.

ABPersonViewController in Modal

I would like to display ABPersonViewController as a modal instead of pushing it on the navigation stack. I've got this working but to keep a done button present I've had to use an NSTimer to add the button every 0.25 seconds because the done button may be removed when the view appears and is always removed when the app enters the forground. This is a pretty lame hack so I'm wondering if anyone has a better idea :)
I made a subclass of ABPersonViewController that adds the done button and starts the timer on view did load and invalidates it when the view is deallocated.
Here is what my code looks like to show the modal:
- (IBAction)showContactModal:(id)sender{
CNABPersonViewController *personViewController = [[CNABPersonViewController alloc] init];
personViewController.displayedPerson = self.contact.record;
personViewController.addressBook = [[CNAddressBookManager sharedManager] addressBook];
personViewController.viewDelegate = self;
personViewController.shouldShowLinkedPeople = YES;
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:personViewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navigationController animated:YES completion:nil];
}
I had success in doing it like this. Insert this line to add a button to the navigation bar:
personViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Test" style:UIBarButtonItemStylePlain target:self action:#selector(_yourAddressBookAction)];
If this does not solve your problem, please show us the code that you had the issue with.

UIView Modal Popup with Embedded UITableView with UIToolbar, Toolbar moves with Table

This is driving me insane! (ios 5+, ARC)
Pretty simple concept here:
I have a UIView with an embedded UITableView, when clicking on a specific cell, I have iOS launch a modalview with a segue.
This modalview, is a UIView with an Embedded UITableView in it, filled with names from a data source. You can choose multiple items in this view (using cellaccessory: checkmark)
The Goal
Get some sort of "Done" button to show up
Ok, so after much running around, I am to understand that Modal windows do not, in fact, allow navigationController items. No toolbars, no Navigationbars by default.
Ok, so I'll create my own.
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger tbHeight = 50;
UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - tbHeight), self.view.frame.size.width, tbHeight)];
tb.translucent = YES;
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneAction:)];
NSArray *barButton = [[NSArray alloc] initWithObjects:flexibleSpace,doneButton,nil];
[tb setItems:barButton];
[self.view addSubview:tb];
barButton = nil;
//....
}
Simple enough right?
Well, the toolbar does show up, for sure. BUT, it doesnt stick to the bottom like its supposed to. In fact, when you scroll the embedded tableview up and down, the UIToolbar goes with it, almost like its stuck on the tablevie somehow.
Ive been looking for hours for solutions, I've come across nothing. Wondering if anybody here has any ideas?
If you need more information, please by all means, enquire :)
It's strange that your toolbar is scrolling with the table if it's a UIViewController subclass, unless you've assigned a UITableView to self.view or something... But since it is, this is what I do when adding a fixed item to a table view:
-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
tb.frame = CGRectMake(0, self.view.bounds.size.height-tb.bounds.size.height+scrollView.contentOffset.y, self.view.bounds.size.width, tb.bounds.size.height);
}

Have a textField input show as a label on a different view?

Like my question states. I want to have a textField and have a user type something in and press a button and it shows as a label on different view? What would I use to do. Could you give an example in code form. Plus it is going to be like 28 different TextFields?
Set a delegate for your text field. The textFieldDidEndEditing: method of the UITextFieldDelegate protocol then allows you to intercept the event that occurs once the text field has finished editing.
Your implementation of textFieldDidEndEditing: can then instantiate a UILabel (or some other type of view) that contains the text that the user input into the text field.
-(IBAction)clickButton:(id)sender{
if (!createViewController) {
createViewController = [[CreateViewController alloc] initWithNibName:#"CreateViewController" bundle:nil];
}
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[self.navigationController pushViewController:createViewController animated:YES];
}
and pass yourTextfield.text to CreateViewController and assign to yourLabel.text
I think it will be helpful to you.