How to refresh navbar so that latest title is displayed? - objective-c

In my implementation, I will change the title of the page after triggering some method
//In viewdidload of my view controller which resides in a navigation controller
-(void)viewDidLoad
{
self.title = #"Title A";
}
-(void)changeTitle
{
self.title = #"Title B";
}
In the above example, after I trigger the 'changeTitle' method, I do not see my viewcontroller's title change immediately. In fact I need to push another viewcontroller onto the stack and subsequently press 'back' before I see the change to "title B".
Is there any way to refresh the navbar's title at the point where I change title?

I guess you are looking for:
UrNavController.navigationBar.topItem.title = #"bkjasdkahsdflkjadf";
You can reset it after you press a button.
You may also assign some other objects to that topitem like a segmentcontrol, or uilabel.

If you want to refresh the previous viewController's title then use
self.parent?.title = "Some Title"

Objective-C
[self.navigationItem setTitle:#"XYZ"];
Swift 4.2
self.navigationItem.title = "XYZ"

Related

Objective C - removing default chevron glyph for back button in iOS SDK 8.4

i'm seeking help in removing the default "<" symbol placed before the back button text in a navigation controller going from ViewControllerA to ViewControllerB.
in ViewControllerA i have:
-(void) viewDidLoad
{
[super viewDidLoad];
self.navigationItem.backBarButtonItem.title = #"Back to A"
self.navigationItem.backBarButtonItem.image = nil;
}
the result - when segued into ViewControllerB - is: "< Back to A" in the new navigation controller whereas I want just "Back to A".
any help is much appreciated.
Are you using storyboards? If you are, you get the desired result by drag-dropping a UIBarButtonItem to where the default back button shows up. That's it. Set the desired text and arrow will not show up.
EDIT:
To make the custom back button function, connect an IBAction to it.
in view controller B:
- (IBAction)backToControllerA {
[[self navigationController] popViewControllerAnimated:YES];
}
Connect the action to the button in IB.

UIPageViewController within NavigationController

I've read every tutorial I've found about UIPageViewController, but they show just basics, I'd like to create something like new twitter app has:
UIPageViewController is embedded into Navigation controller, title of navigation bar is based on current page and those page dots are there as well, user can tap on item on current page(item from table view/collection view) to see detail.
I was able to come up with something similar, each page had collection view, and showing detail of some item was reflected in navigation bar, there was correct title and "<" button, but I wasn't able to change title based on currently shown page
Please, could you describe me how to do this in few steps/basic structure of controllers?
Don't know if you are still working on this but here goes anyway. To set up a UIPageViewController you might use the tutorial and two questions below.
http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/
How to implement UIPageViewController that utilizes multiple ViewControllers
How to add UIBarButtonItem to NavigationBar while using UIPageViewController
The last link pertains specifically to setting the contents of the navigationBar depending on what you are viewing.
The key is to create a UINavigationItem Property in the .h file of your UIPageViewController content view controllers, meaning the ones/one that are displaying whatever it is you are displaying.
From my code in FirstViewController.h SecondViewController.h and ThirdViewController.h
#property (strong, nonatomic) UINavigationItem *navItem;
In the second and third links above you'll see a storyboard layout of a Master-Detail application (which uses a navigation controller). The UIPageViewControllerDataSource is the DetailViewController. The three pages associated with the pageViewController are my content view controllers.
In DetailViewController.m you have to instantiate the contentViewControllers somewhere. At that point you pass the DetailViewControllers navigationItem id to the content view controllers. Here is how I instantiate my content view controllers using the delegate methods of the UIPageViewController.
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSString * ident = viewController.restorationIdentifier;
NSUInteger index = [_vc indexOfObject:ident];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
if (index == 0) {
return [self controllerAtIndex:index];
}else if (index == 1){
return [self secondControllerAtIndex:index];
}else if (index == 2){
return [self thirdControllerAtIndex:index];
}else{
return nil;
}
}
The delegate method calls the method below. It is almost directly from the tutorial link with just a few modifications.
-(FirstController *)controllerAtIndex:(NSUInteger)index
{
FirstController *fvc = [self.storyboard instantiateViewControllerWithIdentifier:#"FirstPageController"];
fvc.imageFile = self.pageImages[index];
fvc.titleText = self.pageTitles[index];
fvc.pageIndex = index;
fvc.navItem = self.navigationItem;
return fvc;
}
Notice that properties are passed into the view controller including self.navigationItem. Passing it in ensures you can make changes to the navigationBar items.
Then in the viewDidAppear method of your content view controller you can simply set the title on the navigation bar like this.
navItem.navigationItem.title = #"Whatever you want the title to be";
It is important to use viewDidAppear because viewDidLoad is not called every time the screen appears. I believe the UIPageViewController caches the page before and the page after whatever you are viewing which saves the system from having to load the page every time you navigate to it.
If you are using a single view controller for all you pages like the tutorial does you will have to use the index property to know what to set the title to.
Hope this helps!
I had a very similar setup and solved the problem.
My setup is that I have a UIPageViewController inside a UINavigationController because I wanted the navigation bar to be persistent while I swiped between each view controller. I wanted the title of the current UIViewController inside the UIPageViewController to become the title of the UINavigationController.
The way to do this is to implement the UIPageViewControllerDelegate method pageViewController didFinishAnimating which triggers after a change to a view controller is made from the UIPageViewController. You can probably see where this going: From here, set the navigationItem.title property of the UIViewPageController, which the UINavigationController uses to set it's own title, with that of the current view controller's title.
Example:
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
if(finished && completed)
{
NSString *titleOfIncomingViewController = [[pageViewController.viewControllers firstObject] title];
pageViewController.navigationItem.title = titleOfIncomingViewController;
}
}
NB: This delegate method triggers only off gesture-initiated scrolls.
Solution given by Mr. Micnguyen is the exact solution but the mentioned delegate method didFinishAnimating() is called when swipe action is done due to which initially title is not shown.
Hence to resolve that problem, we need to set its initial value in viewDidLoad() method of UIPageViewController class as mentioned below:
- (void)viewDidLoad(){
self.navigationitem.title = arrayname[0];
}
I had this question too, but ended up doing something different, because I'm using Swift, so I thought I'd share here.
I ended up embedding a UIPageViewController in a Container View in my Navigation Controller. On a page swipe, I used pageViewController(_:didFinishAnimating:previousViewControllers:transitionCompleted:), with my PageViewController as the UIPageViewDelegate. From there I created a protocol that I used to send data about the VC displayed to the parent VC, and change the title using self.title = "My Title".
I didn't make the parent VC the UIPageViewDelegate because I found it to be easier to access the displayed VC from the PageViewController than from the parent VC as let childVC = pageViewController.viewControllers![0] as! DetailViewController.

"back" text displayed in iOS7 UINavigationBar when view title is long

In my application,I need to show the previous viewController title to current viewController back title.
Its working perfectly in iOS6.
In iOS7,automatically the "back" title displayed other than the previous viewController title.
how to fix the issue in iOS7?
In iOS 7 you will not be allowed to set the back button's title to be any longer than 11 characters.
To avoid changing the title of the view controller, but to change the back button's title, you need to do this:
In the previous view controller (the one that will have the next view controller pushed on top of it) you need to set the backBarButtonItem like so:
/**
* Notifies the view controller that its view was added to a view hierarchy.
*
* #param animated If YES, the view was added to the window using an animation.
*/
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.title = #"My Title Can Be Long";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"ThisIsLimit"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
}
Now, when the next view controller is pushed on top of it, the back button will be whatever title you put in the backBarButtonItem.
Due to low reputation I cannot add a comment so I'm posting an answer while this is not actually an answer.
But,
self.navigationController.navigationBar.topItem.title = #"";
which is written in one of the answers, is equivalent to:
self.title = #"";
try this,
self.navigationController.navigationBar.topItem.title = #"";
iOS 7 will automatically replace your back button title with "Back" or even remove the title altogether in order to fit the title of current navigation item. You probably shouldn't try to do anything about it except maybe try and make your titles shorter.
if you want to make short title you can do as below
self.title = #"SOME REALLY LONG NAVIGATION BAR TITLE";
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
label.text=self.navigationItem.title;
label.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=label;

How to specify the left, right, or title View button in XIB

I do not want to create a custom navigation bar.
I am pushing a UIViewController and I want to customize how the navigation bar looks for that UIViewController
In story board, we just specify the segue and a nav bar show up on the screen. We just drag and drop UIBarItem to the left and right.
In XIB, the navigationBar simply doesn't show up.
I can add navigation Item but the one I added is ignored.
I've heard that there used to be an outlet called navigationItem but it's deprecated for reason I do not know.
I can add UINavigationBar, however that would be adding my own custom bar. I want the navBar that's provided by UINavigationController.
The appearance of the Navigation Bar in the storyboard designer is just there to illustrate how your screen will look when you load your ViewController inside a UINavigationController. It doesn't mean that you actually have a navigation controller in your app.
You need to add a UINavigationController to your storyboard (probably as the first scene), and then connect your ViewController to it (as the root view controller).
Then you should be able to set your title in the storyboard designer, and drag bar button items onto the navigation bar.
See also the answer to this question.
You can setup it in code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Title";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back"style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationItem.rightBarButtonItem = "Create button" ;
self.navigationItem.leftBarButtonItem = "Create button" ;
self.navigationItem.titleView = "Create custom title view";
}
I think this is the actual way apple want this to be implemented.
Put UINavigationBar
Set outlet to the UINavigationItem
This is the catch
Override navigationItem property to return the UINavigationItem you created.
That's it.
-(UINavigationItem *) navigationItem
{
return self.navigationItem1;
}
If your navigationItem is still in the UINavigationBar, I think you will need to have a strong outlet to the UINavigation Bar too. Please correct me if I am wrong here.

Setting title of UINavigationBar

I have an application which uses a main story board to include a Navigation Controller where the main view is a Table View using a prototype cell content. Each cell in the table view pushes onto a new view which I have created with it's own set of .h .m and .xib files.
The table view navigation bar has its title set through the story board which works fine. However, I am having trouble setting the title for each new view after it gets pushed into view.
I have the following in the viewDidLoad method for each view;
self.title = #"View Title";
Any advice?
it is the right way. it will work.
I'm doing something similar in an app I'm working on. Here's how I'm setting the title:
[[self navigationItem] setTitle:#"My View's Title"];
I thinkt the dot notation equivalent would be something like this:
self.navigationItem.title = #"My View's Title";
Hope that helps.
If you have a tab bar controller, try this:
self.tabBarController.navigationItem.title = #"Title";