Which NavigationController Method runs on every view reload (TabBarItem) - ios

I have a NavigationController then a TabBarController which has Four Tabs.
I wanted to display Different titles on TopBar when a Different Tab is selected.
One way was to Embed each TabBarItem View into Navigation Controller but for some reason this doesn't seems the correct way, i wanted to apply this via code.
I managed accomplish this by using this code: (Products_ViewController.m custom class)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UINavigationController *navCon = (UINavigationController*) [self.navigationController.viewControllers objectAtIndex:0];
navCon.navigationItem.title = #"Products";
}
But the problem is now when a tab is clicked First time, it changes the title but then it doesn't. I then applied the same code on -(void)viewDidAppear{} but still the same result.
How can i manage to display navigation top bar title (or run the above code) whenever the tab bar item is clicked or the view is shown?
Thanks!

You could implement the UITabBarControllerDelegate in the Products_ViewController.m class and execute your code in the tabBarController:didSelectViewController: method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
UINavigationController *navCon = (UINavigationController*) [self.navigationController.viewControllers objectAtIndex:0];
navCon.navigationItem.title = #"Products";
}
In the viewDidLoad method you have to set the delegate to self.

Related

UINavigationController hide nav bar for single viewcontroller

I knew its a duplicate. But still having an issue and even when tried with possibilities didn't work. Hence posting the same to reach a solution. Hope to get help from you guys.
The initial is embedded inside UINavigationController. For the initial (the landing view) the navigation bar must be hidden. The other views when called from the landing view - must show the navigation bar.
I'm handling the hide & show of navbar in the landing view by overriding the methods of the view as follows:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Hiding the navigationbar hidden for the first page
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
// Even tried animated:NO & animated:animated
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
// Showing the navigationbar hidden for the first page
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
While the app loads initially, the nav bar is in hidden state (as expected & working fine). When coming back to the landing view from the child view controller, the nav bar gets hidden after some seconds - the landing view gets loaded on to the ui screen.
I also tried using the navigationcontroller delegate method in landing view: navigationController: willShowViewController: animated:. But unable to reach the solution that i need.
Hence i provided the navigationcontroller delegate in one of my childviewcontroller and checked whether the childcontroller when popped is not in viewcontrollers of the navigationcontroller using if condition. When yes, then i provided the hide option of the navigationbar. but also failed to have the solution.
During surfed, there was a solution to handle with viewanimation. I tried and that too failed.
Again surfed, the solution provided across is to handle the similar issue with viewwillappear & viewwilldisappear. I'm blinked since the way i'm doing is similar to the proposed way. Even then unable to reach a solution.
FYI.. I'm using Xcode 6.3 and deployment target is 6.0 onwards. I'm using storyboard to manage views.
Please help me sort the issue... App loads is hiding the nav bar in landing page. But when landing page is loaded back from a child view then the nav bar gets hidden only after the landing page loaded on to the ui. I do need to get hidden of the nav bar as like when app loads, when the child view pops and the landing view gets loaded on the top of the controller.
If you want to hide navigation bar in the second view then don't try to manage in viewWillAppear and viewWillDisappear because I have faced a lot of problems by trying like that and it also effected the constraints. Just use delegate for navigation controller in appDelegate it is working fine for me.
self.navigationController.delegate = self;
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([viewController isKindOfClass:[LoginViewController class]])
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
} else {
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
}
Use this method:
[navigationController setNavigationBarHidden:YES];
So, if you are in some view controller:
[self.navigationController setNavigationBarHidden:YES];
More clarifications:
UINavigationController has a property navigationBarHidden, that allows you to hide/show navigation bar for whole nav controller.
Let's look at the next hierarchy:
--UINavigationController
----UIViewController1
----UIViewController2
----UIViewController3
Each of three UIViewController will have nav bar since they are in UINavigationController. For example, you want to hide bar into the second (actually it doesn't matter in which one), then write into UIViewController2:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES]; //it hides
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO]; // it shows
}
Overwrite the delegate method in your custom UINavigationController class:
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[self setNavigationBarHidden:NO];
if ([viewController isKindOfClass:[SomeViewController class]])
{
[self setNavigationBarHidden:YES];
}
}
One advantage for putting it in your UINavigationController class is that you don't clutter your UIViewController class with code
Tested and works.
UPDATE
Create a UINavigationController subclass: f.e. MyNavigationController
In AppDelegate.h:
#import "MyNavigationController.h"
#property (nonatomic) MyNavigationController *navigationController;
Then initialise it in AppDelegate.m:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Probably some more code here
self.navigationController = [[MyNavigationController alloc] initWithRootViewController:yourRootViewController];
self.window.rootViewController = self.navigationController;
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
return YES;
}
Then overwrite the delegate method in your custom UINavigationController class
I have little to no experience with storyboards so not really sure how to setup a custom UINavigationController, but this is how I do it in code.
Here's another SO post how to create a custom UINavigationController to use with storyboards.

How to keep the tab bar hidden while switching between more than 5 tabs

I have a UITabBarController as my root view controller for my app. It has 6 tabs but the app has a custom popup view with 6 buttons used to selected each of the tabs. The tab bar itself is hidden at all times.
The problem is once I try to programmatically select a tab at index 5 or 6 I get an issue. Tabs 1-4 are fine, they get selected in code and the new view controller appears on screen. But since tabs 5 & 6 are technically in the "more" tab, the tab bar appears briefly, shows animation to select the "more" tab and then disappears again. This also puts these "extra" view controllers in a new navigation controller with the "more" table view as the root view controller. This adds a new navigation bar and causes other issues.
Is there any way to do any of the following?
Have more than 5 tabs in a tab bar without the "more" tab.
Disable the "more" tab bar selection animation and the addition of the associated navigation controller.
Create a simple custom controller that could replace the UITabBarController entirely.
It seems like there are a lot of situations where one would want to show more than 5 tabs and yet hide the tab bar but I couldn't find anyone discussing this issue.
According to your requirements, I think you need a custom tabar controller.
This project may help you:
RDVTabBarController
Besides, I must warn you that using custom tabbar controller you may lost the chances to use the convenient features that system tabber contrller provides.
You should use the custom tabbar controller only if the system tabbar controler doesn't fit you needs.
Try the code below . The ViewController used here is a sub class of UITabBarController. In the .h file add ITabBarDelegate , UITabBarControllerDelegate.I guess this way you can add 6 tabs. I have done two tabs here and transition with animation. Use the delegate method
(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
as shown below to apply animations.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
fvcontroller = [self.storyboard instantiateViewControllerWithIdentifier:#"navcontroller"];
svcontroller = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
NSMutableArray *viewcontrollers = [[NSMutableArray alloc]init];
[viewcontrollers addObject:fvcontroller];
[viewcontrollers addObject:svcontroller];
[self setViewControllers:viewcontrollers];
fvcontroller.tabBarItem = [[UITabBarItem alloc]initWithTitle:#"Me" image:[UIImage imageNamed:#"me.png"] tag:1];
svcontroller.tabBarItem = [[UITabBarItem alloc]initWithTitle:#"Chat" image:[UIImage imageNamed:#"chat3.png"] tag:2];
// _tbbar.delegate = self;
self.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
UIView *from = self.selectedViewController.view;
UIView *to = viewController.view;
NSInteger fromindex = [self.viewControllers indexOfObject:self.selectedViewController];
NSInteger toindex = [self.viewControllers indexOfObject:viewController];
[UIView transitionFromView:from
toView:to
duration:.5
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:^(BOOL finished) {
if (finished) {
tabBarController.selectedIndex = toindex;
}
}];
//(toindex > fromindex ? UIViewAnimationOptionTransitionCurlUp : UIViewAnimationOptionTransitionCurlDown)
return NO;
}
#end

iOS: Call actionsheet from tabbar

I've looked at all other answers for this topic on Stackoverflow but don't get really further. I've set up my Tabbar controller in Storyboard. I've defined the icons for the tabbar items also in Storyboard, the titles however I've set via code in their respective view controllers since my app offers multi-language support.
Now I want one single tabbar button which doesn't segue to another view but just calls an actionsheet. No matter on which other tabbar I am. So my questions are:
Where do I add this tab bar button? Because all other buttons I can only define after creating the segue to the new view controller
Where do I place the code for the action sheet?!
I'm not very clued up on the tabbar, however i would do the following:
Assuming you are using a TabBarController View or a Central View With a TabBar in it.
In the -viewDidLoad
NSMutableArray *tmp = [[NSMutableArray alloc] initWithArray:self.tabBarController.viewControllers];
UIViewController *sheet= [[UIViewContoller alloc] init];
sheet.title = #"Sheet";
sheet.tabBarItem.image = [UIImage....];
[tmp addObject:sheet];
[self.tabBarController setviewControllers:tmp];
self.tabBarController.delegate = self;
Then place the following
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if([viewController.Title isEqualToString:#"Sheet"])
{
//ActionSheet create code here
retrun NO;
}
else
{
return YES;
}
}

Change title of navbar based on which tab is selected?

I have an iOS app where there is a navigation controller as the root controller but at one part there is a tab bar to select between views in one of the nav bars. It looks similar to the iTunes app (navigation bar on top, tab bar on the bottom). I want the title of my navigation bar to change based on which tab is selected. I have two separate controller files for each tab. Here is what I have tried to use in each so far to fix this to no avail:
self.navigationItem.title = #"Title";
self.navigationController.navigationItem.title = #"title";
[self.navigationController setTitle:#"Live"];
[self setTitle:#"Top Title"];
How do I change the NavBar title based on which tab is pressed?
You change the title of the bar in the view controller that is currently being displayed.
So for example, in view controller A that you're showing in the tab controller, you might add:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[self setTitle:#"A"];
self.tabBarController.navigationItem.title = #"A";
}
Same goes for B, C, etc.
In your ViewControllers that are in the tabs:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.tabBarController.title = self.title;
}
If the individual view controllers presented by the tab bar controller have their own navigation bars, then
[self setTitle:#"Foo"];
will set both the tab bar label, as well as the navigation bar title.
If the navigation controller is at the top level (i.e. the tab bar is inside the navigation controller), then you might have to set the navigation bar title's manually (and you'll want to do this in viewDidAppear rather than viewDidLoad, because these child controllers are not reloaded every time you switch), e.g.:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.navigationController.navigationBar.topItem setTitle:#"Foo"];
}
Alternatively, you could do this navigation bar title adjustment in your UITabBarControllerDelegate method didSelectViewController.
If this doesn't do it, you might have to clarify your question, describing the hierarchy of controllers (e.g. is the tab bar controller inside navigation bar, or vice versa).
You can subclass the UITabBarController, set the delegate to itself, and use the delegate to set its title when the view controller is selected:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//change the title based on viewController that is selected
self.title = #"New title";
}
Just Two Lines of Code..Only thing is, you need to use viewWillAppear method
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tabBarController.navigationItem.title = #"Your Title";
}
PS: Inspired from Above Answers...
UITabBarController *tabController = (UITabBarController *)self.parentViewController;
tabController.navigationItem.title = #"ABC";
This is working for me
From some R&D on internet
You have to pass the navigationItem up the chain.
The UINavigationController shows the navigationItem belonging to its topViewController which is UITabBarController.
The UITabBarController shows the navigationItem titles in its tabs. So what you need to do is make sure that the tabBarController's navigationItem is it's selectedViewController's navigationItem
So to recap:
UINavigationController title = topViewController.navigationItem.title
UITabBarController tabTitle =
selectedViewController.navigationItem.title
UIViewController title = navigationItem.title

How to structure ViewControllers using Storyboard for SplitView iPad app?

I'm trying to build the interface for an app that looks like this:
I want to use the Storyboard to create the views, but I'm having trouble figuring out how now. I've dropped a SplitViewController that is hooked up to 2 navigation controllers: MasterNavigationController, and DetailNavigationController (Master is the left side menu and Detail the right side).
I was starting to build all my ViewControllers in storyboard. Then build NSArrays to hold the various ViewController stacks needed for each of the Master's menu items. So when a user taps on a menu item, I'd load the corresponding ViewController stack into the DetailNavigationController using this method:
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
But now I'm thinking, I should create a separate DetailNavigationController for each of the menu items. For example:
RecentOrdersNavigationController
CustomersNavigationController
ItemsNavigationController
...
Then when a user taps a menu item, the entire DetailNavigationController changes to the appropriate one.
How should I be structuring the the interface while using storyboard?
Use a separate viewController for each detail view. You want your code to be separate since it can get confusing fast with all the different functionality for each view. From there, you can easily swap out the the detail.
Subclass UISplitViewController and set your root splitViewController to that class. Then add this method to your UISplitViewController subclass:
-(void)setDetailControllerTo:(UIViewController *)detailController withNavControllerTitle:(NSString *)title {
[detailController view]; // this line forces the viewDidLoad method to be called
if (title) {
UINavigationController *navController = [[UINavigationController alloc] init];
[navController pushViewController:detailController animated:YES];
detailController.title = title;
NSArray *viewControllers=#[self.mainController.viewControllers[0],navController];
self.mainController.viewControllers = viewControllers;
} else {
NSArray *viewControllers=#[self.mainController.viewControllers[0],detailController];
self.mainController.viewControllers = viewControllers;
}
}
To call this method do something like this from the master view controller in the tableView:didSelectRowAtIndexPath: method
FixedSplitViewController *splitController = (FixedSplitViewController*) self.splitViewController;
CurrentEventViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"CurrentEventViewController"];
// add any setup code here
[splitController setDetailControllerTo:controller withNavControllerTitle:#"Current Event"];
If you wish to keep the master view visible in portrait rotation, add this method to the SplitViewController subclass:
-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
return NO;
}
A lot of my projects require the splitviewcontroller to always show the master view so I use this subclass to keep the master view from hiding on portrait rotation.

Resources