How to deal with a presentModal inside a tabBar that needs to use navigationController - ios

I'm trying to push a view controller inside a presentmodal but nothing happening. So, I have a loginController as rootViewController that push a tabBarControler then inside one of tabBar views I open a presentModal. In the presentModal I have an UITableView when clicked push another view but when I try do this nothing happen. To try be more clear here's the hierarchy:
LoginViewController(as root view controller) >>>push>>> UITabBarController(HomeViewController and two others) >>>open>>> PackViewController(presentModal) that needs to open another view by pushViewController:
The code:
AppDelegate.m
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
LoginViewController_iPhone *loginViewController = [[LoginViewController_iPhone alloc] init];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:loginViewController] autorelease];
self.window.rootViewController = self.navigationController;
LoginViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
HomeViewController_iPhone *referenciaHomeViewController = [[HomeViewController_iPhone alloc] init];
MapaViewController_iPhone *referenciaMapaViewController = [[MapaViewController_iPhone alloc] init];
GuiaViewController_iPhone *referenciaMidiaViewController = [[GuiaViewController_iPhone alloc] init];
UINavigationController *navHome = [[UINavigationController alloc] initWithRootViewController:referenciaHomeViewController];
UINavigationController *navMapa = [[UINavigationController alloc] initWithRootViewController:referenciaMapaViewController];
UINavigationController *navGuia = [[UINavigationController alloc] initWithRootViewController:referenciaMidiaViewController];
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tabBarBackground"]];
[self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"tabBarSelected"]];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navHome, navMapa, navGuia, nil];
}
- (void)login {
[self.navigationController pushViewController:self.tabBarController animated:YES];
}
PackViewController.m (PRESENTMODAL):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
CompraPackViewController_iPhone *controller = [[CompraPackViewController_iPhone alloc] init];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}

When you do your present modal, you need to present a navigation controller whose root view controller is the table view controller.

Related

Navigation Bar not showing title

I am unable to get the navigation bar to show the title that I specify. I have tried to change the title in the AppDelegate.m, as well as in the viewDidLoad of my first tab view. I suspect the title is being hidden, but I am unable fix it. Please provide any insight you may have.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];
UIColor *firstColor = [UIColor colorWithRed:155.0f/255.0f green:41.0f/255.0f blue:104.0f/255.0f alpha:1.0f];
UIColor *secondColor = [UIColor colorWithRed:215.0f/255.0f green:90.0f/255.0f blue:18.0f/255.0f alpha:1.0f];
NSArray *colors = [NSArray arrayWithObjects:firstColor, secondColor, nil];
[[CRGradientNavigationBar appearance] setBarTintGradientColors:colors];
self.navigationController.title = #"This title is not showing";
[[navigationController navigationBar] setTranslucent:NO];
//create the view controller for the first tab
self.firstViewController = [[FirstViewController alloc] initWithNibName:nil
bundle:NULL];
//create the view controller for the second tab
self.secondViewController = [[SecondViewController alloc] initWithNibName:nil
bundle:NULL];
//create the view controller for the third tab
self.thirdViewController = [[ThirdViewController alloc] initWithNibName:nil
bundle:NULL];
//create the view controller for the fourth tab
self.fourthViewController = [[FourthViewController alloc] initWithNibName:nil
bundle:NULL];
//create an array of all view controllers that will represent the tab at the bottom
NSArray *myViewControllers = [[NSArray alloc] initWithObjects:
self.firstViewController,
self.secondViewController, self.thirdViewController, self.fourthViewController, nil];
//initialize the tab bar controller
self.tabBarController = [[MainUITabBarController alloc] init];
//set the view controllers for the tab bar controller
[self.tabBarController setViewControllers:myViewControllers];
[navigationController setViewControllers:#[self.tabBarController]];
[self.window setRootViewController:navigationController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
The relationship of your view controllers isn't right.
The root view controller of your app should be the TabBarController.
Each of the items should have it's own Navigation Controller.
The title of each ViewController can be set in viewDidLoad() with self.title = "....
I've been able to set titles in the delegate as follows:
1.) Create view controllers
2.) Set those titles
3.) Then create UINavigationControllers and assign view controllers there
self.firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
self.secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
self.thirdViewController = [[ThirdViewController alloc] initWithNibName:nil bundle:nil];
self.fourthViewController = [[FourthViewController alloc] initWithNibName:nil bundle:nil];
[self.firstViewController setTitle:#"First"];
[self.secondViewController setTitle:#"Second"];
[self.thirdViewController setTitle:#"Third"];
[self.fourthViewController setTitle:#"Fourth"];
UINavigationController *controller1 = [[UINavigationController alloc] initWithRootViewController:self.firstViewController];
UINavigationController *controller2 = [[UINavigationController alloc] initWithRootViewController:self.secondViewController];
UINavigationController *controller3 = [[UINavigationController alloc] initWithRootViewController:self.thirdViewController];
UINavigationController *controller4 = [[UINavigationController alloc] initWithRootViewController:self.fourthViewController];
NSArray *viewControllers = [NSArray arrayWithObjects:controller1, controller2, controller3, controller4, nil];

Put a navigation controller in a tabbar controller

I have tried to add a navigation controller to a tab bar controller but the tab appear black.
My code:
AppDelegate.m
#import "SettingsNavigationControllerViewController.h"
#import "SettingsViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2, *viewController3;
SettingsNavigationControllerViewController *viewController4;
UINavigationController *navigationController = [UINavigationController alloc];
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
viewController3 = [[ShareViewController alloc] initWithNibName:#"ShareViewController" bundle:nil];
SettingsViewController *settingViewController = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
viewController4 = [[SettingsNavigationControllerViewController alloc] initWithRootViewController:settingViewController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, navigationController];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
thank you in advance
This is because you added a navigationcontroller with no RootViewController and even doesnt initialized. Thats why the view appears as black. May be you mean to add viewController4 instead of navigationController . So change your code as below
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, viewController4];
You need to first create the navigation controllers with rootviewcontrollers
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
Then instantiate the Tab Bar controller like this
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];
Create Navigation Controllers first.
UINavigationController *nav1 = [[UINavigationController alloc] init];
UIViewController *viewController1 = [[[FirstSteps alloc] initWithNibName:#"FirstView" bundle:nil] autorelease];
nav1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
UINavigationController *nav2 = [[UINavigationController alloc] init];
UIViewController *viewController2 = [[[Profiles alloc] initWithNibName:#"SecondView" bundle:nil] autorelease];
nav2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
Then initialize your tabbar controller like this.
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2 ,nil];
self.window.rootViewController=self.tabBarController;
Hope this helps you.
Hey mate please visit the below link for tab bar example.
I think this is good and nice example which would be helpful to you.
https://github.com/alikaragoz/AKTabBarController
You only have to set your controllers name in appDelegate.m file

How to Show LoginViewController over SplitViewController?

I am working on application for iPad which have CustomTabBar (3 Tabs), Navigation Bar and SplitView Controller.
Follow of application screens:
1-Login Screen (Present over SplitView) >> Forgot password (Push on login navigation)
2- Forget password if pushed on login navigation then it will be poped and then login will be dismissed on successful login.
3- On Successful Login, SplitView is shown.
How I code in AppDelegate:
self.detailViewController = [[[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:self.detailViewController] autorelease];
customTabBarCont = [[CustomTabBarController alloc] init];
self.splitViewController = [[[UISplitViewController alloc] init] autorelease];
self.splitViewController.delegate = self.detailViewController;
NSArray *viewControllers = [[[NSArray alloc] initWithObjects:customTabBarCont,detailNavigationController, nil] autorelease];
self.splitViewController.viewControllers = viewControllers;
self.detailViewController.splitViewController = self.splitViewController;
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
loginVC = [[[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil] autorelease];
UINavigationController *loginNavigationController = [[[UINavigationController alloc] initWithRootViewController:loginVC] autorelease];
[self.splitViewController presentViewController:loginNavigationController animated:NO completion:nil];
I have SettingViewController in which i have given the functionality of Sign-out.
It create issues when user Signout from Portrait and Login in LandscapeView
How i code for SignOut
self.loginVC = [[[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil] autorelease];
if ([app_delegate.detailViewController.masterPopoverController isPopoverVisible]) {
[app_delegate.detailViewController.masterPopoverController dismissPopoverAnimated:NO];
}
UINavigationController *loginNavigationController = [[[UINavigationController alloc] initWithRootViewController:self.loginVC] autorelease];
When we rotate again then everything goes well. but how to solve this issue. or where i am wrong.
You just have to add one method in your DetailViewController.m
- (BOOL)splitViewController:(UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
return NO;
}
The run you project and see what happens. Surly this will solve your problem

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];

xCode 4.2 UITableView drilldown

I'm a newbie to the xCode world and I had a few questions regarding my application setup.
My application a list of Authors, click on author and get author detail plus book titles, and then click on the book title and get book information, but I'm unable to figure out the show detail part.
I have established a tab view controller that displays UIViewControllers in the window.
//create view controllers
UIViewController *vc1 = [[HomeViewController alloc] init];
UIViewController *vc2 = [[AuthorViewController alloc] init];
UIViewController *vc3 = [[BooksViewController alloc] init];
UIViewController *vc4 = [[GenreViewController alloc] init];
UIViewController *vc5 = [[UserViewController alloc] init];
//create instance of tab bar
self.tabBar = [[UITabBarController alloc] init];
//add views to tab bar
self.tabBar.viewControllers = [NSArray arrayWithObjects:vc1,vc2,vc3, vc4, vc5, nil];
self.window.rootViewController = self.tabBar;
//self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];
[_window addSubview:_tabBar.view];
[self.window makeKeyAndVisible];
return YES;
This works perfectly. My first view "AuthorViewContoller" is a table and I can display data, however I can NOT get the detailController to show.
My AuthorViewController viewDidLoad method
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.author= [[NSArray alloc] initWithObjects:#"One", #"two", nil];
self.detailController = [[AuthorDetailController alloc] init];
and my methoddidSelectRowAtIndexPath:
if(indexPath.row == 0)
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
AuthorDetailController *dc = [[AuthorDetailController alloc] initWithNibName:#"AuthorDetailController" bundle:nil];
dc.title = [author objectAtIndex:indexPath.row];
[self.navigationController pushViewController:dc animated:YES];
}else{
[self.navigationController pushViewController:detailController animated:YES];
}
I'm declaring detailController in my AuthorViewController.h file.
#property (nonatomic, retain) IBOutlet AuthorDetailController *detailController;
You have to actually create a UINavigationController, and have it be part of your controller hierarchy, before you can use pushViewController:animated:. You should really try setting this up in a NIB, instead of code, but in your code you can try this in place of your current vc2 initialization:
UIViewController *vc2 = [[UINavigationController alloc] initWithRootViewController:[[AuthorViewController alloc] init]];

Resources