i added tabs but views outside tabs became unreachable (ios) - ios

my original app had 5 home screen views and buttons in the home screens would open different other views.
i am not using storyboards. orignally there were no tabs in the app. then i added tabs to display the home screen using
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] ;
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
UIViewController *viewController4 = [[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil];
UIViewController *viewController5 = [[FifthViewController alloc] initWithNibName:#"FifthViewController" bundle:nil];
UINavigationController *navigationController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
[navigationController1.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
[navigationController2.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
[navigationController3.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
[navigationController4.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
[navigationController5.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UITabBarController *tabBarController= [[UITabBarController alloc] init];
tabBarController.viewControllers = #[navigationController1, navigationController2,navigationController3,navigationController4,navigationController5];
//self.window.rootViewController = self.tabBarController;
UIWindow *window = [[UIApplication sharedApplication] delegate].window;
[window addSubview:tabBarController.view]
;
but now when i click the buttons on the homescreen how do i show the views which are outside the tabs ? and how do i comeback from that view to the 5 homescreen tab view.
earlier i was using this code to show the views on button press
listController = [[MenuListController alloc] initWithNibName:#"MenuListController" bundle:nil];
SUP101AppDelegate *delegate6 = [[UIApplication sharedApplication] delegate];
[delegate6.navController pushViewController:listController animated:YES]

You can launch other viewControllers by simply presenting them on the tabController now.
listController = [[MenuListController alloc] initWithNibName:#"MenuListController" bundle:nil];
SUP101AppDelegate *delegate6 = [[UIApplication sharedApplication] delegate];
[self.tabBarController presentViewController:listController animated:YES completion:nil];
you should also set the tabController to be window RootViewController
self.window.rootViewController = tabBarController;
//[window addSubview:tabBarController.view]

Related

navigation controller without using story boards

SecondViewController *testAppViewController2Obj =[[SecondViewController alloc]initWithNibName:#"TestAppViewController2" bundle:nil];
[self.navigationController pushViewController:testAppViewController2Obj animated:YES];
ContainerViewController *container = [[ContainerViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:container];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 65)];
container.navigationItem.title = #"Frames";
self.window.rootViewController = nav;
i hope it works
In order to push a new view controller you should first create a view controller, VC1, link it with a navigation controller and then link the navigation controller to the rootViewController in the AppDelegate
#AppDelegate
FirstViewController *VC1 = [FirstViewController new];
UINavigationController *nVC = [UINavigationController alloc] initWithRootViewController:VC1];
self.window.rootViewController = nVC;
Then when VC1 gets pushed, you can create another view controller and push it like this:
#Now in VC1
SecondViewController *VC2 = [SecondViewController new];
[self.navigationController pushViewController:VC2 animated:YES];

Show UITabBar without beeing RootViewController

I'm having an LoginViewController which is the RootViewController. This ViewController should not have the TabBar included. The Rest of the ViewController should contain the UITabBar, But it is not showing. If i make the tabBar the rootController it will show the tabBar in the viewcontrollers. This would also make the firsttab the rootviewcontroller which it should not.
My question is then how can i make the loginview the rootViewController without an tabBar and still show the tabBar in the other viewcontrollers?
My code:
tabBarController = [[UITabBarController alloc] init];
MenuViewController *firstTab = [[MenuViewController alloc] initWithNibName:#"MenuViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstTab];
FixtureViewController *secondTab = [[FixtureViewController alloc] initWithNibName:#"FixtureViewController" bundle:nil];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondTab];
WorldCupViewController *thirdTab = [[WorldCupViewController alloc] initWithNibName:#"WorldCupViewController" bundle:nil];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:thirdTab];
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:loginView];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[navController, navController2, navController3];
navController.tabBarItem.image = [UIImage imageNamed:#"message-7"];
navController2.tabBarItem.image = [UIImage imageNamed:#"football-32"];
navController3.tabBarItem.image = [UIImage imageNamed:#"trophy-32"];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:110/255.0f green:89/255.0f blue:196/255.0f alpha:1.0f]];
[self.window setRootViewController:navController4];
[self.window makeKeyAndVisible];
What you need to do is transition the application from the LoginViewController to the Tabbar. What I would suggest doing is replacing the LoginViewController with the TabBar as the rootViewController.
Here is some sample code, perform this action in your AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:loginView];
[self.window setRootViewController:navController4];
[self.window makeKeyAndVisible];
return YES;
}
-(void)transitionToTabBar
{
// Set the TabBar how you are in your sample code, this is just an example.
[self.window setRootViewController:[[UITabBarController alloc] initWithNibName:#"SomeNib" bundle:Nil]];
[UIView transitionWithView:self.window duration:0.5f options:UIViewAnimationOptionTransitionCurlDown animations:^{
[self.window makeKeyAndVisible];
} completion:nil];
}
You should use main View controller (for MenuViewController) and by using segue you can present another view controller which has TabBar in it.

How to make a tab bar application out of standalone NIBs?

I have 5 standalone table view controller nibs (with custom cell implementation) accessible through an another table view menu list (no storyboards)
The client desires to have all 5 nibs in tabs. So I need to get rid of the menu list and provide nibs in TABs .
how can I do this ?
First add this property to your AppDelegate.h
#property (strong, nonatomic) UITabBarController *tabBarController;
make a method to set the views and set up your tabbar like:
-(void)setViews
{
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] ;
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
UIViewController *viewController4 = [[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil];
UIViewController *viewController5 = [[FifthViewController alloc] initWithNibName:#"FifthViewController" bundle:nil];
UINavigationController *navigationController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
[navigationController1.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
[navigationController2.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
[navigationController3.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
[navigationController4.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
[navigationController5.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
[navigationController1.navigationBar setHidden:YES];
[navigationController2.navigationBar setHidden:YES];
[navigationController3.navigationBar setHidden:YES];
[navigationController4.navigationBar setHidden:YES];
[navigationController5.navigationBar setHidden:YES];
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController.tabBar setBackgroundColor:[UIColor clearColor]];
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar"];
[[[self tabBarController]tabBar]setSelectionIndicatorImage:[UIImage imageNamed:#"transparent.png"]];
[self.tabBarController setDelegate:self];
self.tabBarController.viewControllers = #[navigationController1, navigationController2,navigationController3,navigationController4,navigationController5];
self.window.rootViewController = self.tabBarController;
}
avoid the set images and setHidden if you don't want to or not want to make the custom navigation bar.
and call this method in your didFinishLaunchingWithOptions.
Now set up the delegate method for tabbar and you can set the custom images over there:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 0)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-1"];
}
else if (tabBarController.selectedIndex == 1)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-2"];
}
else if (tabBarController.selectedIndex == 2)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-3"];
}
else if (tabBarController.selectedIndex == 3)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-4"];
}
else if (tabBarController.selectedIndex == 4)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-5"];
}
}
I'd set it all up in the main nib/storyboard, but it's easier to show in code. You can create the view controllers in the usual way (again, nib or storyboard or code).
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:#[vc1, vc2, vc3, vc4, vc5] animated:YES];
UIWindow *window = [[UIApplication sharedApplication] delegate].window;
[window setRootViewController:tabBarController];

Navigationcontroller is hidden after pushing viewcontroller

I'm working on an app and I face a problem with UINavigationcontroller.
First in the app delegate I check if the user is logged in, if so I take him to Main screen, if notI take him to the login screen.
This is my code:
UINavigationController *diabetesNavigationController = [UINavigationController alloc];
LoginViewController *loginPage = [[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil];
MainViewController *mainPage = [[MainViewController alloc]initWithNibName:#"MainViewController" bundle:nil];
if ([DataStore instance].userIsLoggedIn)
diabetesNavigationController = [diabetesNavigationController initWithRootViewController:mainPage];
else
diabetesNavigationController = [diabetesNavigationController initWithRootViewController:loginPage];
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
[[UINavigationBar appearance]setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance]setTitleTextAttributes:navbarTitleTextAttributes];
[self.window setRootViewController:diabetesNavigationController];
The problem when the user logs out, he goes back to loginscreen but without the navigationcontroller.
I made something in MainViewController, which is this:
-(void)viewDidAppear:(BOOL)animated
{
self.navigationItem.title = #"Diabetes";
UIBarButtonItem *settingButton=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"burger.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(onBurger:)];
self.navigationItem.rightBarButtonItem = settingButton;
self.navigationItem.leftBarButtonItem.enabled = YES;
}
So when I logout the user, with this code:
LoginViewController *loginPage = [[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil];
[self.navigationController setViewControllers:[NSArray arrayWithObjects:loginPage, nil] animated:YES];
I get to the login screen but without Navigationcontroller. I tried to set Hidden property to NO in all view delegates, but it still has this issue.
You can try this:
UIWindow *window = [UIApplication sharedApplication].keyWindow;
LoginViewController * loginPage = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: loginPage];
[window.rootViewController presentViewController:nav animated:YES completion:NULL];
Let me know if it works.. :)

The correct way of adding a UINavigationController to a existing UIViewController

I'm trying to add a UINavigationController to my existing view controller by adding this in ViewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
navController = [[UINavigationController alloc]init];
[self.view addSubview:navController.view];
}
But by doing it this way, it completely blocks my view. It puts a UINavigationBar on the top but the rest of the view doesn't respond to input.
This is how I am presenting the view. The SecondViewController is the one I want to have a NavController:
UITabBarController *tabController = [[UITabBarController alloc] init];
FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"CardsViewController" bundle:nil];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:#"First"
image:[UIImage imageNamed:#"img1.png"] tag:1];
[viewController1 setTabBarItem:tab1];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"ShoppingViewController" bundle:nil];
UINavigationController *SecondViewNavCont = [[UINavigationController alloc]initWithRootViewController:viewController2];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:#"Second"
image:[UIImage imageNamed:#"img2.png"] tag:2];
[SecondViewNavCont setTabBarItem:tab2];
UIViewController *viewController3 = [[UIViewController alloc] init];
UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:#"Third"
image:[UIImage imageNamed:#"img3.png"] tag:3];
[viewController3 setTabBarItem:tab3];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2,
viewController3,
nil];
[self.view addSubview:tabController.view];
You cannot add it to the current view controller
What you need to do is instead of presenting the ViewController is to add this viewcontroller to the navigationview controller and present that
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:YourPresentedViewController];
//then present the navController
[self presentModalViewController:navController animated:YES];
Now when you present it do the following
NSArray arrayWithObjects:viewController1,
SecondViewNavCont,
viewController3,
nil];
You can try this code for load uiviewcontroller to uinavigationcontroller:
yourviewController *viewcontroller=[[yourviewController alloc] initWithNibName:#"yourviewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller];
[self presentModalViewController:navController animated:YES];
(or) you want to load uinavigationcontroller in application startup try below code in your application delegate class:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller.view];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Welcome!
You need to set the RootViewController for the navController object. Then further you can push UIViewcontroller objects using pushViewController method.
You need to add a view for that Navigation Controller. The added Navigation Controller has no view so it was overlaying on the UIViewController
You need to add
[[UINavigationController alloc] initWithNibName:#"ViewControllerName" bundle:nil];

Resources