Changing Tab bar programmatically - ios

I have created tabbar in storyboard .and i am trying to change default tab bar programmatically using following code.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"TAB"])
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UITabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:1];
}
}
But its not working.How i can do it? its not working at all.
Also i want to go back to home view controller when user select first tab.how we can achieve this functionality.

From the current selected view controller you can change tab selected index using code below.
[self.tabBarController setSelectedIndex:1];
To add Tab bar controller to viewcontrollers hirarchy IF YOU ARE USING NAVIGATION AS BASE then you can use:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UITabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:1];
[self.navigationController pushViewController:tabBar animated:BOOL];

You can change tab from programmatically:-
[self.tabBarController setSelectedIndex:1];
You can access tab bar's object from storyboard. In storyboard select tabBarController and set storyboard Id. Refer image.
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil]; //please check your main storyboard name.
UITabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:1];

Related

why use storyboard push a viewcontroller shows black screen

I created a SingleView application then use "Embed In Navigation Controller" to get a navigation control.
when push a controller, the viewcontroller's backgroundColor is black.
i know use this code :
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];//UIStoryboard(name: "Main", bundle:nil)
UIViewController *roomController = [storyBoard instantiateViewControllerWithIdentifier:#"controlerID"];
but what's the reason ?
and if i don't want to use the storyboard completely ,only use this code
UIViewController *roomController = [[UIViewController alloc]init];
what can i do? is only set the viewcontroller's backgroundColor?
You need to use the instantiateViewControllerWithIdentifier: method of your UIStoryboard instance and then you can push the controller.
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *roomController = [storyboard instantiateViewControllerWithIdentifier:#"controlerID"];
[self.navigationController pushViewController: roomController animated:YES];
// if You make controller with out storyboard Than change You view color
UIViewController *roomController = [[UIViewController alloc]init];
[self.navigationController pushViewController: roomController animated:YES];
- (void)viewDidLoad
{
self.view.backgroundColor =[UIColor whiteColor];
}

after call presentViewController tab bar is missing

test1 is the first view in the tarbarcontorller..
after I call presentViewController tab bar is missing
How can i solve it??
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
TestTableViewController *TestTableViewController = [storyboard instantiateViewControllerWithIdentifier:#"test1"];
TestTableViewController.memberid = memberid;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:TestTableViewController];
[self.navigationController presentViewController:navigationController animated:YES completion:nil];
You should present your TabBarController, and not the first view of your tab bar controller. It will display the first view in your tab bar controller properly.

Navigation and Tab Bar Controller not showing up

I'm trying to use instantiateViewControllerWithIdentifier in the app delegate to show a view controller but for some reason the navigation bar and tab bar isn't show up. I'm not sure what I'm doing wrong - Thanks
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *view = (UINavigationController *)[sb instantiateViewControllerWithIdentifier:#"ShopViewController"];
self.window.rootViewController = view;
Is ShopViewController a UINavigation controller or is it just a view controller. It sounds like you are declaring the view controller as uinavigationcontroller.
Instead you should either drop a navigation controller on the storyboard and then give that an identifier OR you could just create a navigation controller in the app delegate.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ShopViewController *showViewController = (ShopViewController *)[sb instantiateViewControllerWithIdentifier:#"ShopViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:shopViewController];
self.window.rootViewController = nav;

self.navigationController is 'null' even after embed in a navigationcontroller

I want to add a navigationcontroller to an existing viewcontroller which is created using storyboard, i have embed it in a navigation controller, but the code for navigating (shown below) is not working even after embed in the navigation controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self.navigationController pushViewController:nextViewController animated:YES];
When i have tried to log self.navigationController, it shows null.
Update: It is fine when i am trying with presentViewcontroller , but i
want to push the viewcontroller with navigationController.
I am struggling with this for two days,Please help.
If self is a subclass of UINavigationController, you do not need to refer to the navigationController property. (Note that this won't work if EditProfileViewController is also a subclass of UINavigationController, as you can't push a UINavigationController inside a UINavigationController).
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self pushViewController:nextViewController animated:YES];
Otherwise, if you don't have a pre-existing navigation controller
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self presentViewController:nextViewController animated:YES completion:nil];

Confused about storyboards and pushing programmatically ios Objective C

I am really confused about the relationship between storyboards and pushing to views programmatically.
I am using SWRevealViewController to display a menu of items.
If I push to the storyboard using
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PhotosViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"PhotosViewController"];
[self presentModalViewController:controller animated:YES];
[self.navigationController pushViewController:controller animated:YES];
All of the information in my storyboard is displayed but there is no "back" button to the SWRevealViewController.
If I push to the view controller using
PhotosViewController *frontViewController = [[StreamScreen alloc] init];
newFrontController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
Then I can view everything that I have added programmatically but nothing from the storyboard.
My question is how can I access things from both storyboard and things Ive added programmatically.
if you present the view controller then it will not give you default back button because when you present a controller it will not added in navigation stack of NavigationController so it won't give you that option.
If you want push controller do not use presentModalViewController.
Try like below
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PhotosViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"PhotosViewController"];
[self.navigationController pushViewController:controller animated:YES];
and if you want to present controller then create manually add a back button like we have default in navigation back button and on it's click write below code to dismiss the controller.
[self dismissViewControllerAnimated:YES];
Hope this helps you.

Resources