Presenting TabBarController's second Tab from NavigationController - ios

I'm trying when I successfully pass from entry viewController (LoginViewController), the Tab Bar open the second tab item. I know how to do it if the Tab Bar was the entry point of the app, with this code in appDelegate.m file:
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
[tabBar setSelectedIndex:1];
but this doesn't help me a lot this time.
I start thinking maybe a solution is to create a custom class for Tab Bar Controller but I m not sure how to do it.
Right now when the user successfully login in, this line of code controllers the segue between LoginViewController(button) and TabBarController.
[self performSegueWithIdentifier:#"loginToTabBarSegue" sender:self];

I found this answer on stackoverflow by DP2 which is the solution of my problem.
This is the line of the code I put in login button when the user successfully login in.
UITabBarController *loadTabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"TabBarViewControllerID"];
loadTabBar.selectedIndex=1;
[self presentViewController:loadTabBar animated:YES completion:nil];
Thanks everyone for your help!

You're using UITabBarController incorrectly. A tab bar controller should never be embedded into a navigation controller. From Apple's docs:
Because the UITabBarController class inherits from the UIViewController class, tab bar controllers have their own view that is accessible through the view property. When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html
It seems like you want a user to log in before they can use your app. And that the tab bar controller contains the bulk of your app's UI. Consider setting the Tab bar controller as the root view controller of your app and then having the user log in through a modally presented LoginViewController.

Try this to get the tab bar:
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
[nav.topViewController.tabBarController setSelectedIndex:1];

Related

UITableViewcontroller in UINavigationController ios

I have a UITableViewcontroller as a rootviewcontroller for a UINavigationController. the UITableViewcontroller comes with a default edit button on the navigation bar. i am trying to replace this edit button with the default back button that usually shows up in a navigation controller. How can I get the default back button in the tableviewcontroller?
The back button shows up once you pushed another UIViewController onto the UINavigationController's view controller stack! It's automatically added by UINavigationController and the title that it displays will be the title property of the former UIViewController.
i understood my mistake. i noticed that the navigation controller of the rootviewcontroller was null.
below code helped in eliminating this error.
UINavigationController *navCMsgs = [[UINavigationController alloc] initWithRootViewController:ntvc];
[navCMsgs setViewControllers:[NSArray arrayWithObject:self]]
however, i have to explicity unhide the navigation bar as the setting for my rootviewcontroller is hidden

iOS how to go from xib file to a view controller in tab bar

i have a tab bar application
i am using storyboards and also navigation bar in my app
in my app
FirstViewController (in tab bar) with a button go to the SingleViewController (xib file and not on tab bar)
in SingleViewController there is a button too. and i want to go to one of the controllers which is on tab bar
in singleviewController's button method i have this code:
AylikGirisViewController *controller = [[AylikGirisViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
it goes to the controller but i see a black screen and empty screen also moving to the AylikGirisViewController gets hard (i mean very slowly)
so i think my way is wrong
any ideas for the right way?
if you are using storyboard, you can simply drag another view controller onto the storyboard and give name as SingleViewController, you can easily push the SingleViewController onto the FirstViewController if you embed the FirstViewController in UINavigationController. Use storyboard segue to push the controller. Then you can call UITabBarController Delegate method to show the different viewController.

iOS Storyboards - Re-use UIViewController

I've got the following structure setup in my Storyboard.
I've got a TabViewController (circled in red) that shows a UIViewController via one of its tabs by doing a push (circled in blue).
I want to re-use that UIViewController from the TabViewController. I'd like to 'push' it but I don't really have a navigation controller so I may end up displaying it as a modal.
However, I'm not sure how to handle navigation back to the TabViewController since in this case there's no navigation bar. Any suggestions on the best way to handle this?
EDIT
Is there a way to insert a Navigation controller when its displayed directly from the TabViewController?
Why don't you put your view hierarchy like this:
UITabBarController -> UINavigationController -> BlueViewController
This UINavigationController should be put in the viewControllers property of the UITabBarController instance. If you do this, you can push and pop as many view controller as you want and you can also hide the navigation bar if needed.
Cancel button whose action is [self dismissViewControllerAnimated:YES completion:nil];

Redefining rootViewController from a ViewController

I´m making an application that have an wizard at the first time the app runs.
After that, it should present a UITabBarController.
The second time the user runs the app, it should just present the UITabBarController.
Since UITabBarController should be the rootViewController, is there a way to redefine the app rootViewController so, when the wizard ends, the TabBarController shows up??
Or is there a better way to accomplish that behavior?
Thanks!!
Yes, you can redefine the window's root view controller from any controller that's currently displayed. You get the reference to the window with self.view.window. So, when your wizard ends, just instantiate your tab bar controller and change the root:
UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:#"tbc"]; // or other instantiation method if not using a storyboard
self.view.window.rootViewController = tbc;
In the app delegate, put logic that determines whether this is the first run of the app, and if not, run this slightly modified code to launch the tab bar controller directly:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UITabBarController *tbc = [sb instantiateViewControllerWithIdentifier:#"tbc"]; // or other instantiation method if not using a storyboard
self.window.rootViewController = tbc;
Use an unwind segue. When presenting your application, push UITabBarController onto the navigation stack. If this is the first run of your app, disable animation when pushing the tab bar controller, and push the first screen of your wizard on top of it. If it is not the first run, push the tab bar controller with animation.
When the user goes through the wizard to completion, use an unwind segue to go back to the tab bar controller.

How to show modalviewcontroller above tabbar

I have tab bar and in view "A" and in "A" I have navigation controller. SO inside my navigation controller in "A" i called
[self presentModalViewController:modalView animated:YES]
But modalView shows under tab bar. How to show it above tab bar?
Try to present modal view from UITabBarController:
[self.tabBarController presentModalViewController:modalView animated:YES];
My case, the modal has a transparent background, I set modalPresentationStyle is .overFullScreen and it show at above tabbar with clear background.
In my case the presented view controller had UIModalPresentationStyle.CurrentContext at .modalPresentationStyle, which made the tab bar overlap
Jus switch back to a default value to fix the issue
The cause of this is that the ancestor viewController is not correctly set.
for instance imagine:
UIViewController * myController = ... // a view controller without a proper ancestor
now:
myController.tabBarController == nil
Therefore:
[myController presentModalViewController:otherController];
Will fail (by showing up under the tab bar). The fix is to add myController to its ancestor via
[parentController addChildViewController:myController];
Now, parentController must be added to another controller in the same way and so forth until the root one is your tabController. This is only available in iOS 5+. If build for iOS 4 or earlier, you will have to work around this by making sure all of your controllers are directly added to either a UINav or UITab controller. If this is not possible, you will have to access the UITabBarController via a global variable.

Resources