I have a navigation controller which has view controller(VC1) . This view controller has 3 button out of which 1 directs to a tab bar controller. From tab1 of the tab bar controller a button is there which navigates to a View controller(vc2).
The problem is VC2 is not displaying the tab bar. How can I make my VC2 display the tab bar.
Navigation Controller—> View Controller-->Tab bar Controller —>Tab1 -> View Controller (does not show tab bar)
I am doing this in IOS
Using a tabbarcontroller within a navigation controller is not recommended by Apple. However it is possible to do so.
In the VC1, write the following code.
UITabBarController *tabBarController = [[UITabBarController alloc] init];
MyStartViewController *startController = [[MyStartViewController alloc] initWithNibName:#"MyStartViewController" bundle:Nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:startController];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.viewControllers = #[navController, viewController2];
[self.navigationController pushViewController:tabBarController animated:YES];
Now within startController, add a UIButton. And in the button action, push the new VC2 from it.
Button action:
- (IBAction)buttonPressed {
MyViewController2 *vc2 = [[MyViewController2 alloc] initWithNibName:#"MyViewController2" bundle:nil];
[self.navigationController pushViewController:vc2 animated:YES];
}
Hope this will serve your purpose.
Related
when we will navigate to another view controller. then using back button popviewcontroller property we can come to first view controller.
but what to do when we using MFSideMenu drawer. while clicking on drawer menu navigate to another screen. then how to come back on first view controller.
for navigating to another viewcontroller. I have used following code.
SCMainMenuViewController *scMainView = [[UIStoryboard storyboardWithName:#"Main" bundle:nil]instantiateViewControllerWithIdentifier:#"scMainMenuController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:scMainView];
[self.menuContainerViewController setCenterViewController:nav];
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed completion:^{
}];
Try this:
HomeView *Home = [[HomeView alloc] initWithNibName:#"HomeView" bundle:nil];
NSArray *controllers = [NSArray arrayWithObject:Home];
self.navigationController.viewControllers = controllers;
I am trying to present a UITableViewController modally on one of my view controllers which is further embedded in a UITabBarController. The code I am using is -
ContactTableViewController *contactsVC=[self.storyboard instantiateViewControllerWithIdentifier:#"contact"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:contactsVC];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
contactsVC.myDelegate = self;
[self.navigationController presentViewController:navController animated:YES completion:nil];
The view is presented just fine the first time I do it but when I navigate to this view again in my app, the view being presented is completely blank.. No navigation bar buttons, no title, nothing..
Can't figure out a reason... Any help is very much appreciated!
UPDATE:
HomeViewController *vc = [[HomeViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
This what I do to navigate to that view again while unhiding the tab bar. Home View is the view that presents the Contacts Table View modally.
I am begineer to iOS application development.
My Application flow is like that
It starts as naviagtion controller then after detail page i make custom tab bar as window root controller
now i have 3 tabs
each tab with different ui
means having
tab1 -> table view
tab2 -> grid view
tab3 -> with multiple tables
now i want each ui controller must have it's own navigation bar
and also it crossponds to associate tab.
Thank you for your precious opinion in advance.
Just create the Custom tab bar controller with three navigation controller for 3 tabs.
Then each navigation controller's root view controller should be your corresponding view controller.
For better understanding please review this image.
Programmatically from Tabbarcontroller context
UINavigationController *navCont1;
UINavigationController *navCont2;
UITabBarController *yourTabbarcontroller;
yourTabbarcontroller = [[UITabBarController alloc]init];
FirstViewController *viewCont1 = [[FirstViewController alloc]init];
navCont1= [[UINavigationController alloc]initWithRootViewController:viewCont1];
UITabBarItem *tab1 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1];
[viewCont1 setTabBarItem:tab1];
SecondViewController *viewCont2 = [[SecondViewController alloc]init];
navCont2= [[UINavigationController alloc]initWithRootViewController:viewCont2];
UITabBarItem *tab2 = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:2];
[viewCont2 setTabBarItem:tab2];
tab.viewControllers = [NSArray arrayWithObjects:navCont1,navnavCont2,nil];
self.window.rootViewController = tab;
I push new StatusViewController(i created UITabbarViewController programmically in it) from rootviewController (UInavigation controller). Now, i want to click on logout button, it push rootviewcontroller but i used below code, it push rootviewcontroller fine but it still tabbar at the bottom.
This code to call rootviewcontroller:
LoginTab *loginView = [[LoginTab alloc] init];
[self.navigationController pushViewController:loginView animated:YES];
[loginView release];
And this code is created UItabbarcontroller in StatusViewController:
self.tab=[[UITabBarController alloc]init];
UploadTab *uploadview=[[UploadTab alloc]initWithNibName:nil bundle:nil];
UINavigationController *uploadTabItem = [[[UINavigationController alloc] initWithRootViewController: uploadview] autorelease];
uploadview.title=#"Uploading";
uploadview.tabBarItem.image=[UIImage imageNamed:#"Uploading.png"];
self.title = #"FirstViewControllerTitle";
//SecondViewController
ConvertTab *convertView=[[ConvertTab alloc]initWithNibName:nil bundle:nil];
UINavigationController *convertTabItem = [[[UINavigationController alloc] initWithRootViewController: convertView] autorelease];
convertView.title=#"Convert";
convertView.tabBarItem.image=[UIImage imageNamed:#"Convert.png"];
//ThirdViewController
CompletedTab *completedView=[[CompletedTab alloc]initWithNibName:nil bundle:nil];
UINavigationController *completedTabItem = [[[UINavigationController alloc] initWithRootViewController: completedView] autorelease];
completedView.title=#"Completed";
completedView.tabBarItem.image=[UIImage imageNamed:#"Completed.png"];
UIBarButtonItem * LogoutItem= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Logout.png"] style:UIBarButtonItemStylePlain target:self action:#selector(logout)];
self.navigationItem.rightBarButtonItem = LogoutItem;
self.tab.viewControllers=[NSArray arrayWithObjects:uploadTabItem,convertTabItem, completedTabItem, nil];
// [self.view insertSubview:self.tab.view belowSubview: uploadview.view];
[self presentModalViewController:self.tab animated:NO];
You can see this image :
Use popToRootViewControllerAnimated method instead of pushViewController:
[self.navigationController popToRootViewControllerAnimated:animated];
Your hierarchy does not seem correct. Your tab bar controller should be the root view controller. For each tab, you can have a navigation controller which has its own controllers to push and pop. That said, your tab bar will always be visible since that is the behavior that is expected when you have a tab bar based app. If you want to present a view which does not show a tab bar, you will need to present that view controller as a Modal view controller on top of your tab bar controller.
I'm developing an iOS app which asks the user to either login or signup before using the functionality. The flow is;
The app displays a screen with 2 buttons 1) Login 2) Signup
Upon clicking either button the app navigates the user to different view controllers using UiNavigationController.
Upon successful login the user is directed towards a home view controller which displays a tab bar on the bottom of the view and a UiNavigationController's top bar on the top of the view.
To implement this, I have set the UINavigationViewController as the root view controller in the AppDelegate file.
As soon as the user is authenticated, I push the tab bar controller on the UiNavigationController using the following code.
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[firstViewController setTabBarItem:[[UITabBarItem alloc] initWithTitle:#"1st" image:[UIImage imageNamed:#"1st.png"] tag:101]];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[secondViewController setTabBarItem:[[UITabBarItem alloc] initWithTitle:#"2nd" image:[UIImage imageNamed:#"2nd.png"] tag:102]];
ThirdViewController *thirdViewController = [[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil];
[thirdViewController setTabBarItem:[[UITabBarItem alloc] initWithTitle:#"3rd" image:[UIImage imageNamed:#"3rd.png"] tag:104]];
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:[NSArray arrayWithObjects:firstViewController, secondViewController,thirdViewController, nil]];
[self.navigationController pushViewController:tabController animated:YES];
Is this the right approach?
Someone just said that the using tab bar controller as root view controller will be a better approach.
How can I just set the tab bar controller as root view controller as I dont want to show the tab bar on login/signup screens....
Thankyou!
Just create a protocol, so you can communicate between your login UIViewController and your AppDelegate. Once the login has been done, you can change the rootViewController. You can also get a reference to the AppDelegate, by doing something like:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
Although I prefer to use protocols to communicate between different objects.