xcode navigation title write programmatically - ios

self.navigationItem.title = #"My Title";
self.title = #"JAJAJ";
I used this codes but not work. Most of time i use this code and was work. But now not work any idea ?

Write either
self.title = #"JAJAJ";
in your init method of your view controller (not in viewDidLoad),
or override:
- (UINavigationItem*) navigationItem
{
UINavigationItem* item = [super navigationItem];
item.title = #"MY Title";
return item;
}
both works.
For explanation, see Apple's Documentation on navigationItem

Related

Issues with UINavigationItem changing properties at runtime

i have a Navigation Bar, wich contains a Navigation Item, which contains 2 Bar Buttons, these are created in the Storyboard, and i wanted to change 1 of the buttons at runtime, now this works:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UINavigationItem *thisNavBar = [self myNavigationItem];
thisNavBar.rightBarButtonItem = nil; // this works, it gets removed
UIBarButtonItem *insertBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(insertSkemaItem:)];
thisNavBar.rightBarButtonItem = insertBtn; // this also works, it sets the btn
}
Now, in my other method, which is called by another controller, it does not work
- (void)callChildChange {
...
// remove old btn
UINavigationItem *thisNavBar = [self skemaNavigationItem];
thisNavBar.rightBarButtonItem = nil; // does not work?
}
There is nothing wrong with the method, it runs just fine, but the nav btn item does not get removed ?
skemaNavigationItem is a Navigation item, declared in the .h file which links the navigation item i made via the storyboard.
Your UI items need to be added to your code (by ctrl-dragging) in the header file (.h) so they can be publicly accessed from other classes/view controllers.
Presuming you've done this, hiding a UI item is best done by using
relevantClass.yourViewObject.hidden = YES;
or if you really need to delete it for good,
[relevantClass.yourViewObject.view removeFromSuperView];
Edits
Options for changing target method:
Declare #property (nonatomic, assign) BOOL myButtonWasPressed; and:
- (IBAction) myButtonPressed
{
if (!self.myButtonWasPressed)
{
// This code will run the first time the button is pressed
self.myButton.text = #"New Button Text";
self.myButtonWasPressed = YES;
}
else
{
// This code will run after the first time your button is pressed
// You can even set your BOOL property back, and make it toggleable
}
}
or
- (IBAction) myButtonWasPressedFirstTime
{
// do what you need to when button is pressed then...
self.myButton.text = #"New Button Text";
[self.myButton removeTarget:self action:#selector(myButtonPressedFirstTime) forControlEvents:UIControlEventTouchUpInside];
[self.myButton addTarget:self action:#selector(myButtonPressedAgain) forControlEvents: UIControlEventTouchUpInside];
}
- (IBAction) myButtonWasPressedAgain
{
// this code will run the subsequent times your button is pressed
}

Passing Data from Slide menu to UITableViewController through UINavigationViewController

How can I pass data from UINavigationController to The root UITableViewController?
I have implemented the ECSlidingViewController (https://github.com/edgecase/ECSlidingViewController). User selects one of the cells in the menu that correspond to different urls I want to display information from on my tableView that sitts on top of the UINavigationController. (u know the default combination that u get my dragging UINavigationController to ur storyboard). I am able to get the data from the sliding menu to my navigationController now I am trying to pass that same info on my tableview?
In my menu I have:
UINavigationController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"NavigationTop"];
newTopViewController = [(NavigationTopViewController*)newTopViewController initWithCinema:self.myCinema];
In UINaviationController:
- (id)initWithCinema:(Cinema *)cinema {
self = [super init];
if(self) {
_myCinema = [[Cinema alloc] init];
_myCinema = cinema;
}
return self;
}
- (void) viewDidLoad {
[super viewDidLoad];
// this log works I get the info to here.
NSLog(#"url(navigation):%#", self.myCinema.cinemaURL);
//MoviesTableViewController *moviesTableViewController = [[MoviesTableViewController alloc] initWithCinema:self.myCinema];
//UITableViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"MoviesTable"];
//NavigationTopViewController *newTopViewController = [[NavigationTopViewController alloc] initWithCinema:self.myCinema];
//newTopViewController = [(MoviesTableViewController *)newTopViewController initWithCinema:self.myCinema];
//[self performSegueWithIdentifier:nil sender:self.myCinema];
[self prepareForSegue:nil sender:self.myCinema.cinemaURL];
}
In my UITableView:
- (void)setCinema:(Cinema *)cinema {
// works here too
NSLog(#"Table(setCinema):%#", cinema.cinemaURL);
self.myCinema = [[Cinema alloc] init];
if(!cinema) {
cinema.cityIndex = kAstanaIndex;
cinema.name = kKeruen;
cinema.nameForText = kKeruenText;
cinema.cinemaURL = kKeruenURL;
cinema.cinemaURLTomorrow = kKeruenURLtomorrow;
}
self.myCinema = cinema;
// works here too!!!
NSLog(#"Table(myCinema):%#", self.myCinema.cinemaURL);
}
However its gone in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// set delegate to self
self.tableView.delegate = self;
// set loading theater's url
// does not work here: I GET NULL !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NSLog(#"url(moviesTable):%#", self.myCinema.cinemaURL);
_model = [[MovieModel alloc] initWithURL:self.myCinema.cinemaURL];
}
None of the methods I have tried (commented in Navigation worked...) at least for me. Please give me any suggestions. Thank you in advance.
UINavigationController does not hold any data, but rather a stack of view controllers. I'd recommend you check out frameworks such as the free Sensible TableView. The framework will automatically handle detail view generation and passing data between them. Saves me tons of development time in my projects.

How can I localize my UITabBarItems?

I'm beginning to learn how to localize iOS applications and hit a wall while trying to localize my UITabBarItems.
Note that these were created in interface builder (using XCode 4).
Is there a way to do this or would I need to create the UITabBarController using just code and manually inserting a localized string for each UITabBarItem?
Cheers
PS:
I do know that I can set the tile of a UITabBarItem by setting the view controller's title like so:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(#"Test", #"");
}
... but this only woks once you hit the tab bar item. Before that it just shows what you put in interface builder...
It seems to work if you set title in awakeFromNib instead:
- (void)awakeFromNib
{
self.title = NSLocalizedString(#"Test", #"");
}
On Swift 3 and 4:
override func awakeFromNib() {
super.awakeFromNib()
self.title = NSLocalizedString("Test", comment: "")
}

How do I hide/show the right button in the Navigation Bar

I need to hide the right button in the Navigation Bar, then unhide it after the user selects some options.
Unfortunately, the following doesn't work:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
Is there a way?
Hide the button by setting the reference to nil, however if you want to restore it later, you'll need to hang onto a copy of it so you can reassign it.
UIBarButtonItem *oldButton = self.navigationItem.rightBarButtonItem;
[oldButton retain];
self.navigationItem.rightBarButtonItem = nil;
//... later
self.navigationItem.rightBarButtonItem = oldButton;
[oldButton release];
Personally, in my apps I make my nav buttons into #properties, so that I can trash & recreate them at will, so something like:
//mycontroller.h
UIBarButtonItem *rightNavButton;
#property (nonatomic, retain) UIBarButtonItem *rightNavButton;
//mycontroller.m
#synthesize rightNavButton;
- (UIBarButtonItem *)rightNavButton {
if (!rightNavButton) {
rightNavButton = [[UIBarButtonItem alloc] init];
//configure the button here
}
return rightNavButton;
}
//later, in your code to show/hide the button:
self.navigationItem.rightBarButtonItem = self.rightNavButton;
For Swift 3
if let button = self.navigationItem.rightBarButtonItem {
button.isEnabled = false
button.tintColor = UIColor.clear
}`
Set reference to nil:
current_controller_in_navcontroller.navigationItem.rightBarButtonItem = nil;
Also be sure to call this in the controller currently shown by the navController, not for the navController itself.
Show:
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
Hide:
[self.navigationItem.rightBarButtonItem.customView setAlpha:0.0];
You can even animate its showing/hiding
[UIView animateWithDuration:0.2 animations:^{
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
}];
If you have only one bar button item in the right side you can use this one,
self.navigationItem.rightBarButtonItem = nil;
Suppose if you have multiple bar button in the right side, for example suppose you have two bar button items(search button and filter button) in the right side of your navigation item. Now the right bar button items are
self.navigationItem.rightBarButtonItems = [searchItem,filterItem]
and you have to hide only search button, you can use like,
self.navigationItem.rightBarButtonItems = [filterItem]
Now what happening is, you can completely hide the search button from the navigation item and the filter item comes in the place of search item
Here's Matt's solution updated for Storyboards & ARC. Remember, IBOutlets are __weak by default, so you need to change that to strong for it not to be released too early.
#interface MAGTableViewController () <UITextFieldDelegate>
#property (strong, nonatomic) IBOutlet UIBarButtonItem *rightBarButton;
#end
#implementation MAGTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setRightBarButtonItem:nil];
}
- (IBAction)rightBarButtonItemTapped:(id)sender
{
[self.view endEditing:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.navigationItem setRightBarButtonItem:self.rightBarButton];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self.navigationItem setRightBarButtonItem:nil];
}
#end
Credit has to go to learner for this answer which the answer is from this question:
hide and show left navigation bar button on demand in iOS-7
This is the answer, which is far more simple.
//hide and reveal bar buttons
-(void) hideAndDisableLeftNavigationItem
{
[self.navigationItem.leftBarButtonItem setTintColor:[UIColor clearColor]];
[self.navigationItem.leftBarButtonItem setEnabled:NO];
}
-(void) showAndEnableLeftNavigationItem
{
[self.navigationItem.leftBarButtonItem setTintColor:[UIColor blueColor]];
[self.navigationItem.leftBarButtonItem setEnabled:YES];
}
Then you just reference the method where you require it like within an (IBAction) like so:
[self hideAndDisableLeftNavigationItem];//[self showAndEnableLeftNavigationItem]; to show again
I tried all other methods and none worked, even referencing my button as a #property (...) UIBarButtonItem.... and nothing worked until I found this.
SWIFT 2.2
In swift 2.2 self.navigationItem does not work. Instead create an outlet of the NavigationItem (I named it below "nav") and use it.
Also the following suggestion did not work for me using Xcode 7.3 and swift 2.2
nav.leftBarButtonItem?.customView?.hidden = true
So I used the idea of #Matt J above as follows (I have 2 items on the left):
Create outlets for the items in the navigation bar and variables to store them
#IBOutlet weak var settingItem: UIBarButtonItem!
#IBOutlet weak var logoItem: UIBarButtonItem!
var sav_settingItem: UIBarButtonItem = UIBarButtonItem()
var sav_logoItem: UIBarButtonItem = UIBarButtonItem()
Save the items in viewDidLoad()
sav_settingItem = nav.leftBarButtonItems![0]
sav_logoItem = nav.leftBarButtonItems![1]
To HIDE set them to nil
nav.leftBarButtonItem = nil
nav.leftBarButtonItem = nil
To SHOW them
if (nav.leftBarButtonItem == nil) {
nav.leftBarButtonItem = sav_settingItem
nav.leftBarButtonItems?.append(sav_logoItem)
return
}
Show:
[self.navigationItem.rightBarButtonItem.customView setHidden:NO];
Hide:
[self.navigationItem.rightBarButtonItem.customView setHidden:YES];
My solution:
self.navigationItem.rightBarButtonItem.customView.hidden=NO;
Swift 2:
Trick!
Hide:
if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
btn.enabled = false
btn.title = ""
}
Show:
if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
btn.enabled = true
btn.title = "ButtonName"
}
For swift 5 to hide rightBarButtonItem
self.navigationItem.rightBarButtonItem?.customView?.isHidden = true
In swift 4 I has a trick to show / hide right or left button:
Step 1: Create a IBOutlet button in view controller:
#IBOutlet var navigationItemButton: UIBarButtonItem!
Step 2: Create Hide button function:
func hideNavigationButton() {
navigationItemButton.isEnabled = false
navigationItemButton.tintColor = UIColor.clear
}
Step 3: Create Show button function:
func showNavigationButton() {
navigationItemButton.isEnabled = true
navigationItemButton.tintColor = UIColor.white
}
Step 4: Just call the functions that you want, use hideNavigationButton() to hide, and showNavigationButton() to show the button.
Regards!
You can use below code:
self.navigationItem.rightBarButtonItem?.image = nil
self.navigationItem.rightBarButtonItem?.isEnabled = false
Show:
//set navigationItem tint color white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
Hide:
//set navigationItem tint clear white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor];
Assume you can reference the specific bar button as variable xxxButton
(please open Assistant Editor, Control+Drag xxx button to YourViewController class as outlet "xxxButton").
or you can use something like let xxxButton = navigationBar.buttons[1]
Hide
xxxButton.customView = UIView()
or
navigationItem.rightBarButtonItems?.remove(at: (navigationItem.rightBarButtonItems?.index(of:xxxButton)!)!)
Show
xxxButton.customView = nil
or
navigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)
Hope helpful.
To hide:
if let topItem = self.navigationController?.navigationBar.topItem {
topItem.rightBarButtonItem = nil
}
Set the the title to empty first and after swlwction just set again.

UINavigationController how to set title

I have a Controller/View for a generic list of items, that can be extended for displaying a custom list.. Listing and navigation works fine.. but I can't change the title of UINavigationController.
In the generic Controller:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview: navigationController.view];
}
- (void)setNavigationTitle: (NSString *)title
{
NSLog(#"set title: %#", title); // this works
self.navigationController.title = title; // Nothing works here
}
Then, the extended class does..
- (void)viewDidLoad
{
[super viewDidLoad];
[self setNavigationTitle: #"Custom list"];
}
The navigationBar still have "Item" as title :(
In your UIViewController there is a title property and it is that property that will be displayed by the NavigationController. So when pushing a new UIViewController onto the navigation stack set the title of that UIViewController to whatever is appropriate.
In your case it looks like it would be:
[self setTitle:#"WhateverTitle"];
For those looking for a Swift solution:
class CustomViewController: SuperViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "My Custom Title"
}
}
The title needs to be set on the UIViewController and not on the UINavigationControllerembedding the view controller.
Documentation: UIViewController Class Reference: title Property
use
self.title = #"yourTitle";
Swift
title = "whateverTitle". It's a UIViewController instance property ; invoke anytime.
From the documentation:
Declaration
var title: String? { get set }
Discussion
Set the title to a human-readable string that describes the view. If the view controller has a valid navigation item or tab-bar item, assigning a value to this property updates the title text of those objects.
I know its old thread but thought of sharing this
Set the 'self.title' in 'init' method in UIVIewControler derived class,
This worked for me!!

Resources