Load all Viewcontrollers when application opens - ios

I'm working on a tabbed application for iOS. Because the tabbar icons are loaded in each viewcontroller separately, I was wondering if it is possible to load all the viewcontrollers at once in the background so all the tabbar icons are loaded when the application is launched.
The tabbar makes use of two icons for each tabbar item (selected and unselected), thats why I choose to load the icons in each viewcontroller separately
And otherwise, is there a possibility to load the tabbar icons in the App delegate?

Yes it is possible. You can initialize all controllers and all images in App delegate's didFinishLaunchingWithOptions methods. Here is the example:
UIViewController *locateTabController = [[LocationTabController alloc] initWithNibName:#"LocationTabController" bundle:nil];
UINavigationController *locationTabNavigationController = [[UINavigationController alloc] initWithRootViewController:locateTabController];
// Product Tab
UIViewController *productsTabController = [[ProductsTabController alloc] initWithNibName:#"ProductsTabController" bundle:nil];
UINavigationController *productsTabNavigationController = [[UINavigationController alloc] initWithRootViewController:productsTabController];
// Delivery Tab
UIViewController *nextDeliveryTabController = [[DeliveryTabController alloc] initWithNibName:#"DeliveryTabController" bundle:nil];
UINavigationController *nextDeliveryTabNavigationController = [[UINavigationController alloc] initWithRootViewController:nextDeliveryTabController];
// Order Tab
UIViewController *standingOrderTabController = [[OrderTabController alloc] initWithNibName:#"OrderTabController" bundle:nil];
UINavigationController *standingOrderTabNavigationController = [[UINavigationController alloc] initWithRootViewController:standingOrderTabController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationTabNavigationController, productsTabNavigationController,nextDeliveryTabNavigationController, standingOrderTabNavigationController, nil];
Here navigation controller is provided as every tab controller class had its own navigation.
You can add titles and images at the same time.
/// Adding titles on each of the tab bar controllers
[[self.tabBarController.viewControllers objectAtIndex:0] setTitle:#"Locate"];
[[[self.tabBarController.viewControllers objectAtIndex:0] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"LocateIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"LocateIconInactive.png"]];
[[self.tabBarController.viewControllers objectAtIndex:1] setTitle:#"Products"];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"ProductsIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ProductsIconInactive.png"]];
[[self.tabBarController.viewControllers objectAtIndex:2] setTitle:#"Delivery"];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"NextDeliveryIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"NextDeliveryIconInactive.png"]];
[[self.tabBarController.viewControllers objectAtIndex:3] setTitle:#"Order"];
[[[self.tabBarController.viewControllers objectAtIndex:3] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"StandingOrderIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"StandingOrderIconInactive.png"]];

Sub class your UITabBarController and set the images on the viewWillAppear. I found that this is the cleanest way of doing it.

You dont need to load any viewcontroller when application opens for this purpose. All you have to do is set tabbar icon inside each viewcontroller's initialization method like below:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.tabBarItem.image = [UIImage imageNamed:IMAGE_NAME];
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:IMAGE_NAME] withFinishedUnselectedImage:[UIImage imageNamed:IMAGE_NAME]];
self.tabBarItem.title = TITLE;
}
return self;
}
After that when you initialize the viewcontroller object from the AppDelegate, tabbar icon will be set.
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];

Related

UITabBarController turns Black while switching tabs

I have embedded a UITabBarController inside a UINavigationController. The UITabBarController contains 3 different tabs which leads to 3 different UIViewControllers which displays UITableView. I implemented all these through storyboard. In the first UIViewController, I have implemented a UISearchController for the UITableView programatically which is working. The View turns black when I search for a particular cell and then if I switch tabs.
I believe that the issue has some relation to SearchController because before including the SearchController, I didn't have any issues.
I have gone through few other questions in here, but none could solve my issue.
Edited :
Below is my code related to SearchController.
#interface
#property (nonatomic, strong) UISearchController *searchController;
#property BOOL searchControllerWasActive;
#property BOOL searchControllerSearchFieldWasFirstResponder;
#implementation
viewDidAppear()
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.tblContactsTable.tableHeaderView = self.searchController.searchBar;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
//reloads table view according to what is searched.
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
self.searchController.searchBar.showsCancelButton = NO;
UITabBarController should be the top most element on window from your storyboard/code and you can add the Navigation Controllers and View Controllers to UITabBarController view controllers.
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITabBarController_Class/index.html
use this sample code it will surely help you here chatlist, contactlist and findfriends are 3 UIViewControllers
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// self.navigationItem.hidesBackButton=YES;
self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
_chatlist = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"ChatListVC"];
_contactlist = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"ContactListVc"];
_findfriends = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"FindfriendsVC"];
self.tabbarcontroller = [[UITabBarController alloc]init];
self.viewControllers = [NSArray arrayWithObjects:_chatlist,_contactlist,_findfriends, nil];
self.tabBar.frame =CGRectMake(0,44,self.view.frame.size.width,50);
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];
item0.title = #"Chat List";
item1.title = #"Contacts";
item2.title = #"Find Friends";
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"chatlist.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"chatlist.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"contacts.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"contacts.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"findfriends.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"findfriends.png"]];
}
Figured out the answer myself. I don't know if its the exact right way of doing it but now the App seems working.
I place my UITabBarController as the Initial View and separate UINavigationControllers for each tabs.

IOS: Adding viewcontroller to custom TabBar in ios

i am working on a chat project in which i have to use a UITabBar. I am using storyboard in this project but according to requirement i added custom TabBar in project to mount it on the top of the view.
Now the problem is Custom TabBar is showing well on the top of view but when i click on the tabs it only shows black screen instead of showing the ViewControllers.
i am sharing my code please suggest me the solution of it.
my .h file
#interface TabBar : UITabBarController
{
}
#property(strong,nonatomic) UITabBarController *tabbarcontroller;
#property(strong,nonatomic) ChatListVC *chatlist;
#property(strong,nonatomic) ContactListVc *contactlist;
#property(strong,nonatomic) FindfriendsVC *findfriends;
#end
my .m file
#implementation TabBar
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// self.navigationItem.hidesBackButton=YES;
self.title = #"Chat Home Page";
self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
_chatlist = [ChatListVC new];
_contactlist = [ContactListVc new];
_findfriends = [FindfriendsVC new]; //initWithNibName:#"FindfriendsVC" bundle:nil];//[FindfriendsVC new];
self.tabbarcontroller = [[UITabBarController alloc]init];
self.viewControllers = [NSArray arrayWithObjects:_chatlist,_contactlist,_findfriends, nil];
//self.navigationController.navigationBar.frame.size.height;
//UITabBar *tabBar = self.tabBarController.tabBar;
//CGFloat topBarOffset = self.topLayoutGuide.length;
self.tabBar.frame =CGRectMake(0,44,self.view.frame.size.width,50);
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];
item0.title = #"Chat List";
item1.title = #"Contacts";
item2.title = #"Find Friends";
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"chatlist.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"chatlist.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"contacts.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"contacts.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"findfriends.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"findfriends.png"]];
}
snapshot of the output is
It looks like your view controllers aren't being initialised properly.
If your view controllers are all in the Storyboard, try replacing
_chatlist = [ChatListVC new];
_contactlist = [ContactListVc new];
_findfriends = [FindfriendsVC new];
with:
_chatlist = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"ChatListIdentifier"];
_contactlist = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"ContactListIdentifier"];
_findfriends = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"FindfriendsIdentifier"];
Where the identifier string is your own identifier that you have set up in the Storyboard.

use storyboard with custom UITabbarController

i have been working on this issue for some time now and cannot find a solution to my problem.
i have a tabbar view controller that i have tried to customise with images, i have the custom graphics working however i need to use code to display and init the tabbar's view controllers. i also have a problem with displaying a navigation bar at the top of one of my tabs which i think is connected to how i am initiating the tab view controllers
the storyboard shows that there should be a navigation bar at the top of the medication tab and that the view is connected to the tab bar via a segue
you can see i have tried to use storyboard segues to link my view controllers to the tab bar controller. i have the following code in the MedicationViewController.m
/
// MedicationViewController.m
// fibromapp
//
// Created by jamie mcallister on 08/09/2013.
// Copyright (c) 2013 Jamie McAllister. All rights reserved.
//
#import "MedicationViewController.h"
#import "TakenViewController.h"
#import "MedsListViewController.h"
#import "MedsAlarmViewController.h"
#interface MedicationViewController ()
#end
#implementation MedicationViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
TakenViewController *viewController2 = [[TakenViewController alloc] init];
MedsListViewController *viewController1 = [[MedsListViewController alloc] init];
MedsAlarmViewController *viewController3 = [[MedsAlarmViewController alloc] init];
self.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2,
viewController3,nil];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:#"Medication" image:[UIImage imageNamed:NULL] tag:1];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:#"Taken" image:[UIImage imageNamed:NULL] tag:2];
UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:#"Alarms" image:[UIImage imageNamed:NULL] tag:3];
UIImage* sel = [UIImage imageNamed:#"fmtabSel"];
[viewController1 setTabBarItem:tab1];
[viewController2 setTabBarItem:tab2];
[viewController3 setTabBarItem:tab3];
UIImage* tabBarBackground = [UIImage imageNamed:#"fmtab.png"];
UITabBar *tabBar = self.tabBar;
[tabBar setBackgroundImage:tabBarBackground];
[tabBar setSelectionIndicatorImage:sel];
}
return self;
}
- (void)viewDidLoad
{
UITabBar *tabbar = self.tabBar;
NSLog(#"%f %f", tabbar.frame.size.width, tabbar.frame.size.height);//used to find the size of the bar
[super viewDidLoad];
UIImage* tabBarBackground = [UIImage imageNamed:#"fmtab.png"];
UIImage* sel = [UIImage imageNamed:#"fmtabSel"];
UITabBar *tabBar = self.tabBar;
[tabBar setBackgroundImage:tabBarBackground];
[tabBar setSelectionIndicatorImage:sel];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
with this code i get the tab but there is no navigation bar at the top of this tab.
can anybody suggest what i must do to resolve this?
if you require any more information feel free to ask and i will edit it into the bottom of this question.
thanks in advance :)
To have a Navigation bar, you have to put a UINavigationController between the tabbar controller and the first UIViewController.
All can be done in storyboard without needs of writing a line of code.
If you want the navigation bar to be at the top You should fill your tabbar controller with navigation controllers inited with root controllers, not just plain controllers.
Smth like that:
TakenViewController *viewController2 = [[TakenViewController alloc] init];
MedsListViewController *viewController1 = [[MedsListViewController alloc] init];
MedsAlarmViewController *viewController3 = [[MedsAlarmViewController alloc] init];
UINavigationController * nc1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController * nc2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController * nc3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
self.viewControllers = [NSArray arrayWithObjects:nc1,
nc2,
nc3,nil];

colour icon in UITabbar item objective-c

I have a UITabbar controller with 3 item, I want to have colour icon instead of based Gary icons,
would you please give me some hint that how can I have colour icon in tababr,
Here is my code:
self.title = #"test";
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:self.title image:[UIImage
imageNamed:#"test"] tag:0];
normally test is a icone with a colour picture, but in UITabbar it's just Gary,
Thanks in advance!
Use this code:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"test"]
withFinishedUnselectedImage:[UIImage imageNamed:#"test"]];
You will need to build your own TabBarController. As Per the Apple docs on the matter "This class [UITabBarController] is not intended for subclassing". The docs on the UITabBarItem say that when you are supplying an image for the tab bar "The images displayed on the tab bar are derived from this image". So, whatever image you provide to the tab bar will get manipulated to make it conform to the "normal" look of a tab bar image.
So, you can build a UIViewController with some UIButtons as subviews and then manage the entire look and feel that way.
you have to create 2 images for all 3 tabs one is unselected and second is selected.
after that in your appdelegate.m file write below code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.searchArray = [[NSMutableArray alloc]init];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
viewController1 *view1 = [[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil] autorelease];
view1.tabBarItem.image = [UIImage imageNamed:#"tab-selected-1"];
view1.tabBarItem.title = #"Title1";
viewController2 *view2 = [[[viewController2 alloc] initWithNibName:#"viewController2" bundle:nil] autorelease];
view2.tabBarItem.image = [UIImage imageNamed:#"tab-selected-2"];
view2.tabBarItem.title = #"Title2";
viewController3 *view3 = [[[viewController3 alloc] initWithNibName:#"viewController3" bundle:nil] autorelease];
view3.tabBarItem.image = [UIImage imageNamed:#"tab-selected-3"];
view3.tabBarItem.title = #"Title3";
self.tabBarController.viewControllers = [NSArray arrayWithObjects:view1, view2, view3, nil];
UITabBarItem *tabBarItem1 = [[self.tabBarController.tabBar items] objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"tab-selected-1.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-unselected-1.png"]];
UITabBarItem *tabBarItem2 = [[self.tabBarController.tabBar items] objectAtIndex:1];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"tab-selected-2.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-unselected-2.png"]];
UITabBarItem *tabBarItem3 = [[self.tabBarController.tabBar items] objectAtIndex:2];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"tab-selected-3.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-unselected-3.png"]];
return YES;
}
try this your problem surly solved. Best Of Luck

Navigation Controller loading twice, how to resolve this?

my project contains navigation controller and segmented control (with separate view controller: segmentManagingViewController) programmatically and now i added a tab bar in IB..while calling tab bar controller and navigation controller ,segmentManagingViewController view getting loaded twice.. both in tab bar item1 and in first segment i have called segmentManagingViewController view ....
here is screen shot of my app
and following is
application didFinishLaunchingWithOptions method... please do help me out to resolve this ...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
databaseName = #"breadworks.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
[self readBreadsFromDatabase];
[self categoryFromDatabase];
SegmentManagingViewController * segmentManagingViewController = [[SegmentManagingViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:segmentManagingViewController];
[segmentManagingViewController release];
[self.window addSubview:tabBarController.view];
[window addSubview: navigationController.view];
[window makeKeyAndVisible];
return YES;
}
Your code sounds strange to me.
First, since you use an UITabBarController set it as the rootViewController for your window.
Then, set the UINavigationController as a child controller of your tab bar controller.
Finally, as you did, set the rootViewController for your UINavigationController to segmentManagingViewController.
Now, since I prefer to do it without xib you could do the following.
UITabBarController* tabBarController = [[UITabBarController alloc] init];
SegmentManagingViewController * segmentManagingViewController = [[SegmentManagingViewController alloc] init];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:segmentManagingViewController];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, nil];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
//- only if you don't use ARC -----
[segmentManagingViewController release];
[navigationController release];
[tabBarController release];
//----------------------------------
return YES;
If don't use ARC make attention to memory management!!
Hope it helps.
I declared navigation controller and view controllers (which are used as objects of NSArray) in delegate and created initWithNibName Constructor for view controllers( defining Title, Image and other properties of TabBarItems).. here is the updated code chunks ..
UIViewController *viewController1 = [[AtoZSecondviewController alloc] initWithNibName:#"AtoZSecondviewController" bundle:nil];
UIViewController *viewController2 = [[CategorySecondViewController alloc] initWithNibName:#"CategorySecondViewController" bundle:nil];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController,viewController1 ,viewController2, nil];
following is Definition of initWithNibName in ViewControllers
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self) {
self.title = NSLocalizedString(#"Catogaries", #"Catogaries");
self.tabBarItem.image = [UIImage imageNamed:#"TodaysChoice"];
}
return self;
}

Resources