Im implementing a combined tab bar and navigation programatically, using the apple documentation,
it is not working when calling the initWithFrame,[goes black screen]; but if left as below code it works for showing main screen, with out the tab bar, and when using the tab bar goes black screen
here the code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions {
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
StartViewController *startViewControllerView = [[[StartViewController alloc] init] autorelease]; //ojo recomendado por apple!!!
VideosViewController* VideosViewController_ = [[[VideosViewController alloc] init] autorelease];
PhotosViewController* PhotosViewController_ = [[[PhotosViewController alloc] init] autorelease];
SocialViewController* SocialViewController_ = [[[SocialViewController alloc] init] autorelease];
self.pagesNavigation = [[[UINavigationController alloc] initWithRootViewController:startViewControllerView] autorelease];
self.pagesNavigation.navigationBarHidden = NO;
NSArray* controllers = [NSArray arrayWithObjects:VideosViewController_, PhotosViewController_, SocialViewController_, startViewControllerView, nil];
self.tabBarController.viewControllers = controllers;
[self.window addSubview:startViewControllerView.view];
//self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window makeKeyAndVisible];
return YES;
}
so if left as shown above, it works but if I comment the addSubview and uncomment the initWithFrame, it doesnt work,,
//[self.window addSubview:startViewControllerView.view];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
So, what Im i missing?,
what would be the right way to call the initWithFrame?
thanks a lot!
Why are all your viewcontrollers auto released? You should probably be retaining them and releasing them only when you're done with them.
As for your structure, I've found that building a single navigation controller for each tab in the tabbarcontroller, adding those to the controller, then adding the tabbarcontroller to the window works like this...
AppDelegate.h
property (nonatomic, retain) UITabBarController *tabBarController;
property (nonatomic, retain) UINavigationController *firstNavController;
property (nonatomic, retain) UINavigationController *secondNavController;
property (nonatomic, retain) FirstViewController *firstViewController;
property (nonatomic, retain) SecondViewController *secondViewController;
AppDelegate.m
firstViewController = [[FirstViewController alloc] someInitMethod:someArg];
firstNavController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
secondViewController = [[SecondViewController alloc] someInitMethod:someArg];
secondNavController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
tabBarController = [[UITabbarController alloc] init];
NSArray *tabs = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];
[tabBarController setViewControllers:tabs animated:NO];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
Related
I created tab bar controller and implemented in app delegate but before that i also implemented other view controller.i don't know how to use call them orderly.please tell me how to make them arrange as i like.
#interface AppDelegate ()
{
UINavigationController *navigation;
}
#property (strong, nonatomic) JASidePanelController *viewController;
#property (strong, nonatomic) UITabBarController *this;
#end
#implementation AppDelegate
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
navigation = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
self.window.rootViewController = navigation;
//UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.window = window;
self.viewController = [[JASidePanelController alloc] init];
self.viewController.shouldDelegateAutorotateToVisiblePanel = NO;
self.viewController.leftPanel = [[LeftViewController alloc] init];
UIViewController *viewcontroller1=[[HomeView alloc]init];
UIViewController *viewcontroller2=[[Speciality alloc]init];
UIViewController *viewcontroller3=[[Activity alloc]init];
UIViewController *viewcontroller4 =[[Notification alloc]init];
UIViewController *viewcontroller5 =[[Profile alloc]init];
viewcontroller1.view.backgroundColor = [UIColor whiteColor];
viewcontroller2.view.backgroundColor = [UIColor whiteColor];
viewcontroller3.view.backgroundColor = [UIColor whiteColor];
viewcontroller4.view.backgroundColor = [UIColor whiteColor];
viewcontroller5.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navcontroller1=[[UINavigationController alloc] initWithRootViewController:viewcontroller1];
UINavigationController *navcontroller2=[[UINavigationController alloc] initWithRootViewController:viewcontroller2];
UINavigationController *navcontroller3=[[UINavigationController alloc] initWithRootViewController:viewcontroller3];
UINavigationController *navcontroller4 =[[UINavigationController alloc] initWithRootViewController:viewcontroller4];
UINavigationController *navcontroller5 =[[UINavigationController alloc] initWithRootViewController:viewcontroller5];
viewcontroller1.title = #"Home";
viewcontroller2.title = #"Speciality";
viewcontroller3.title = #"Activity";
viewcontroller4.title = #"Notification";
viewcontroller5.title = #"Profile";
navcontroller1.tabBarItem.image = [UIImage imageNamed:#"home"];
navcontroller2.tabBarItem.image = [UIImage imageNamed:#"special"];
navcontroller3.tabBarItem.image = [UIImage imageNamed:#"activity"];
navcontroller4.tabBarItem.image = [UIImage imageNamed:#"notify"];
navcontroller5.tabBarItem.image = [UIImage imageNamed:#"Pro"];
self.this = [[UITabBarController alloc] init];
self.this.viewControllers=[NSArray arrayWithObjects:navcontroller1,navcontroller2,navcontroller3,navcontroller4,navcontroller5, nil];
self.this.tabBar.barTintColor = RGBCOLOR(249, 178, 131);
self.viewController.centerPanel = _this;
[self.window makeKeyAndVisible];
return YES;
}
Here, you set the window's root controller to a navigation controller:
self.window.rootViewController = navigation;
And then you go on to instantiate a handful of other nav controllers and add them to a tab controller. It's not clear from your question what you're trying to achieve, but if you want your app to use a tab controller, you'll want to set your window's root controller to the tab controller, not a navigation controller.
I am using xib instead of storyboard.
AppDelegate.m
ViewController1 *ViewController1 = [[ViewController1 alloc] init];
ViewController1.title = NSLocalizedString(#"1", #"1");
ViewController2 *ViewController2 = [[ViewController2 alloc] init];
ViewController2.title = NSLocalizedString(#"2", #"2");
BannerViewController *_bannerViewController1;
_bannerViewController1 = [[BannerViewController alloc] initWithContentViewController:ViewController1];
BannerViewController *_bannerViewController2;
_bannerViewController2 = [[BannerViewController alloc] initWithContentViewController:ViewController2];
_tabBarController = [[UITabBarController alloc] init];
_tabBarController.viewControllers = #[_bannerViewController1,_bannerViewController2];
self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
ViewController1.m
#import "ViewController1.h"
#import "ViewController2.h"
ViewController2 *cvc = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
cvc.strReceiveValue= "Testing";
ViewController2.h
#property (strong, retain) NSString *strReceiveValue;
ViewController2.m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"strMortgageAmount: %#", strMortgageAmount);
Any idea why I failed to get the value? I was thinking this could be related to initWithContentViewController.
**
See update below
**
If I understand the question correctly, you are creating a tab bar controller (in AppDelegate?) and wish to pass information to the view controllers associated with the tab bar controller?
If so, take a look at this:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController setDelegate:self];
UINavigationController *dashTVCnav = [[tabBarController viewControllers] objectAtIndex:0];
Then:
DashTVC *dashTVC = [[dashTVCnav viewControllers] objectAtIndex:0];
dashTVC.managedObjectContext = self.managedObjectContext;
dashTVC.uuid=uuid;
Then in DashTVC.h:
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (strong, nonatomic) NSString *uuid;
With this, you pass the managedObjectContect and the uuid to the dashTVC controller.
I hope I understood what you are asking...
+++++++++++++++ UPDATE +++++++++++++++++++++
The original poster was kind enough to give me his sample code. I will post parts of it here with my fix for those who find this through searches in the future
There were several problems, but in short, nothing was being passed to vc2:
1st, nothing was being passed from the appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect bounds = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController1 *vc1 = [[ViewController1 alloc] init];
vc1.title = NSLocalizedString(#"VC1", #"VC1");
ViewController2 *vc2 = [[ViewController2 alloc] init];
vc2.title = NSLocalizedString(#"VC2", #"VC2");
//--- I added this as a test
vc2.strReceiveValue=#"foo";
// and now foo shows up in the debug console when vc2 is fixed
_tabBarController = [[UITabBarController alloc] init];
_tabBarController.viewControllers = #[
[[BannerViewController alloc] initWithContentViewController:vc1],
[[BannerViewController alloc] initWithContentViewController:vc2],
];
self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Next, it was necessary to modify viewWillAppear in viewController2.m
- (void)viewWillAppear:(BOOL)animated {
// line below missing
[super viewWillAppear:animated];
NSLog(#"output: %#", strReceiveValue);
}
Hope this helps, and enjoy developing for IOS!
I am trying to add a Facebook Login function on my app. However, somehow it gives an error saying "'UIViewControllerHierarchyInconsistency', reason: 'adding a root view controller as a child of view controller:'
".
My app delegate is written as follow. Could anyone give me an advice how I could fix the problem?
AppDelegate.h
#property (strong, nonatomic) UITabBarController *tabBarController;
#property (strong, nonatomic) UINavigationController *navigationController;
#property (strong, nonatomic) LoginViewController* loginViewController;
#property (strong, nonatomic) FirstViewController *mainViewController;
AppDelegate.m
self.loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController"
bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.loginViewController];
self.navigationController.delegate = self;
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
//mainwindow
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// ViewControllers array
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
[self resetMainViewController];
//homeview
self.mainViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
//adding navigation controller
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
[viewControllers addObject:self.navigationController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
//secondview
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
//adding navigation
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[viewControllers addObject:navController2];
// Thirdview
ThirdViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
//adding navigationcontroller
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
[viewControllers addObject:navController3];
//fourthview
FourthViewController *viewController4 = [[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil];
//adding navigation controller
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
[viewControllers addObject:navController4];
//fifthviewcontroller
FifthViewController *viewController5 = [[FifthViewController alloc] initWithNibName:#"FifthViewController" bundle:nil];
//adding navigation controller
UINavigationController *navController5 = [[UINavigationController alloc] initWithRootViewController:viewController5];
[viewControllers addObject:navController5];
//tabbarcontroller
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setViewControllers:viewControllers];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
Thank you.
You set self.navigationController to self.window.rootViewController at the beginning, then add it to the viewcontrollers of the self.tabBarController, this is the problem. You should remove all other self.window.rootViewController assignments, and remain the last one.
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]];
How to insert UINavigationController inside UITabBarController.
Currently I have main UITabBarController with declatarion inside application delegate like this (so tab is main)
self.window.rootViewController = self.tabBarController;
And inside one of tabs I want to insert UINavigationController, and can't get it.
The code is contructed like this:
MainWindow.xib with UITabBarController object with tab typed as UINavigationController (point to NavigationHistory.xib) - screenshot: invalid link
NavigationHistory.xib contains only UINavigationController where view point to History.xib
History.xib have only UITableView element - screenshot: invalid link
And now UIViewController doesn't display my View1 view, and I have no clue why it may be. Maybe you have any clue? or point me to the place where such configuration is done.
I'll answer myself. It's good explained at
https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html
Write a code in appdelegate.m file.....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSMutableArray *Mutablearray = [[NSMutableArray alloc] init];
UIViewController *1st_View = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil];
UINavigationController *Navigation = [[UINavigationController alloc] initWithRootViewController:1st_View];
[Mutablarray addObject:Navigation];
UIViewController *2nd_View = [[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil];
UINavigationController *Navigation = [[UINavigationController alloc] initWithRootViewController:2nd_View];
[Mutablearray addObject:Navigation];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = Mutablearray;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Add your controller object to UINavigationController, and add that navigationcontroller object to UITabBarController
UINavigationController is a subclass of UIViewController, a UITabBarController expects an array of UIViewControllers (and therefore UINavigationController ); this tells me me that I can give a UITabBarController an array of UINavigationControllers (or sublasses thereof) giving the desired interaction.
Ref : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tab = [[UITabBarController alloc] init];
SimpleTableViewController *tableView = [[SimpleTableViewController alloc] init];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:tableView];
UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:0];
nav1.tabBarItem = item;
AboutViewController *about = [[AboutViewController alloc] init];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:about];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];
nav2.tabBarItem = item2;
tab.viewControllers = #[nav1,nav2];
self.window.rootViewController = tab;
[self.window makeKeyAndVisible];
return YES;
}