How can we implement a common tabbar for all views in IOS? - ios

I am new to IOS. In my app's home, there are 3 tabs and 4 buttons(using navigation controllers). I created a tabbarcontroller inside appdelegate.m as follows:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//Initializing the view controllers
UIViewController *disController1 = [[DisController1 alloc]initWithNibName:#"DisController1" bundle:nil];
UIViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UIViewController *wishlistController = [[Wishlist alloc]initWithNibName:#"Wishlist" bundle:nil];
UIViewController *helpController = [[HelpController alloc]initWithNibName:#"HelpController" bundle:nil];
UIViewController *disController2 = [[DisController1 alloc]initWithNibName:#"DisController2" bundle:nil];
NSMutableArray *viewControllersArray = [[NSMutableArray alloc]init];
[viewControllersArray addObject:disController1];
[viewControllersArray addObject:viewController];
[viewControllersArray addObject:wishlistController];
[viewControllersArray addObject:helpController];
[viewControllersArray addObject:disController2];
//Initializing the tab bar controller
UIImage *img = [UIImage imageNamed:#"SES_tab bar"];
CGSize newSize = CGSizeMake(SCRN_WIDTH, TABHEIGHT);
Common *common = [[Common alloc]init];
UIImage *newImage = [common imageResize:img andResizeTo:newSize];
[common setTabBarColorandImage:newImage];
tabController = [[UITabBarController alloc]init];
tabController.viewControllers = [NSArray arrayWithObjects:disController1, viewController,wishlistController,helpController,disController2 ,nil];
tabController.selectedIndex = 1;
UITabBarItem *firstTab = [[tabController.tabBar items]objectAtIndex:0];
UITabBarItem *lastTab = [[tabController.tabBar items]objectAtIndex:4];
[firstTab setEnabled:NO];
[lastTab setEnabled:NO];
UINavigationController *navCtrl = [[UINavigationController alloc]initWithRootViewController:tabController];
//navCtrl.navigationBarHidden = NO;
//[navCtrl.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
[navCtrl.navigationBar setBarStyle:UIBarStyleBlackTranslucent ];
[navCtrl.navigationBar.topItem setTitle:#"Home"];
[self.window addSubview:tabController.view];
self.window.rootViewController = navCtrl;
[self.window makeKeyAndVisible];
return YES;
TabBar works fine for my home page.But my problem is when i clicks on a button, Tab bar doesn't appear in the new view. How can I implement that?

You should create UINavigationControllers for each of your tabs. Set each view controller in your current array as the root view controller of each of those navigation controllers.
EDIT:
UIViewController *disController1 = [[DisController1 alloc]initWithNibName:#"DisController1" bundle:nil];
UIViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UIViewController *wishlistController = [[Wishlist alloc]initWithNibName:#"Wishlist" bundle:nil];
UIViewController *helpController = [[HelpController alloc]initWithNibName:#"HelpController" bundle:nil];
UIViewController *disController2 = [[DisController1 alloc]initWithNibName:#"DisController2" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:disController1];
//etc. for each of the view controllers that you have allocated.
NSMutableArray *viewControllersArray = [[NSMutableArray alloc]init];
[viewControllersArray addObject:navController1];
[viewControllersArray addObject:navController2];
[viewControllersArray addObject:navController3];
[viewControllersArray addObject:navController4];
[viewControllersArray addObject:navController5];

Related

How to achieve SplitView Functionality in Portrait mode in iOS?

I'm trying to implement SplitView functionality in my iOS application, it's working fine only in landscape mode in iPad devices.
If I launch or rotate the application from landscape to portrait mode splitView functionality is collapsing.
How to achieve SplitView functionality in iPhone(portrait and landscape) and iPad(Portrait mode) devices?
Please find my codeenter image description here below code.
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
ViewController *masterViewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:#"DetailViewController"
bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
masterViewController.detailViewController = detailViewController;
self.splitViewController = [[CustomSplitViewController alloc]initWithNibName:#"CustomSplitViewController" bundle:nil];
self.splitViewController.viewControllers = #[masterNavigationController, detailNavigationController];
TabBarDetailController *secondVC = [[TabBarDetailController alloc]initWithNibName:#"TabBarDetailController" bundle:nil];
secondVC.tabBarItem.title = #"Second";
ThirdViewController *thirdVC = [[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil];
thirdVC.tabBarItem.title = #"Third";
FourthViewController *fourthVC = [[FourthViewController alloc]initWithNibName:#"FourthViewController" bundle:nil];
fourthVC.tabBarItem.title = #"Fourth";
MoreViewController *moreVC = [[MoreViewController alloc]initWithNibName:#"MoreViewController" bundle:nil];
moreVC.tabBarItem.title = #"More";
//make an array containing view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:self.splitViewController,secondVC,thirdVC,fourthVC,moreVC,nil];
[tabBarController setViewControllers:viewControllers];
self.splitViewController.tabBarItem.title = #"HOME";
[self.window addSubview:tabBarController.view];
self.window.rootViewController=tabBarController;
[[self window] makeKeyAndVisible];
return YES;
}
Thanks in advance.

iOS 9.0 Tabbar hide automatically and viewcontroller disappear in push

I have a TabBarController with 4 Tab. When i push a ViewController from a tab then the tab is hidden automatically and its showing nothing in View. But ViewController contains a tableview with data. I have implemented tableview delegate also.I am not getting tableview delegate logs.
in appdelegate didFinishLaunchingWithOptions()
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.homeScreen = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.rootNav = [[UINavigationController alloc]initWithRootViewController:self.homeScreen];
self.window.rootViewController = self.rootNav;
[self.window makeKeyAndVisible];
Load TabBar here:
myAppDelegate.myTabBarController.selectedIndex = 0;
myAppDelegate.myTabBarController.tabBar.translucent = NO;
myAppDelegate.myTabBarController.tabBar.opaque = YES;
location_select *tab1 = [[location_select alloc] initWithNibName:#"location_select" bundle:nil];
tab1.tabBarItem.image = [UIImage imageNamed:#"Restaurants.png"];
location_select *tab2 = [[location_select alloc] initWithNibName:#"location_select" bundle:nil];
tab2.title = #"Cart";
tab2.tabBarItem.image = [UIImage imageNamed:#"Cart.png"];
Deal_ViewController *tab3 = [[Deal_ViewController alloc] initWithNibName:#"Deal_ViewController" bundle:nil];
tab3.title = #"Deals";
tab3.tabBarItem.image = [UIImage imageNamed:#"Deals.png"];
MoreViewController *tab4 = [[MoreViewController alloc] initWithNibName:#"MoreViewController" bundle:nil];
tab4.title = #"More";
tab4.tabBarItem.image = [UIImage imageNamed:#"More.png"];
myAppDelegate.myTabBarController.viewControllers = [NSArray arrayWithObjects:tab1,tab2,tab3,tab4,nil];
[myAppDelegate.rootNav pushViewController:myAppDelegate.myTabBarController animated:YES];
From Tab 4, i push Profile_ViewController:
Profile_ViewController *vc = [[Profile_ViewController alloc] initWithNibName:#"Profile_ViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:vc animated:YES];
And already i applied there:
myAppDelegate.myTabBarController.hidesBottomBarWhenPushed=NO;
and
[self.tabBarController setHidesBottomBarWhenPushed:NO];
in viewWillAppear of Profile_ViewController. Nothings works. I just see a blank white screen.
location_select *tab1 = [[location_select alloc] initWithNibName:#"location_select" bundle:nil];
tab1.tabBarItem.image = [UIImage imageNamed:#"Restaurants.png"];
location_select *tab2 = [[location_select alloc] initWithNibName:#"location_select" bundle:nil];
tab2.title = #"Cart";
tab2.tabBarItem.image = [UIImage imageNamed:#"Cart.png"];
Deal_ViewController *tab3 = [[Deal_ViewController alloc] initWithNibName:#"Deal_ViewController" bundle:nil];
tab3.title = #"Deals";
tab3.tabBarItem.image = [UIImage imageNamed:#"Deals.png"];
MoreViewController *tab4 = [[MoreViewController alloc] initWithNibName:#"MoreViewController" bundle:nil];
tab4.title = #"More";
tab4.tabBarItem.image = [UIImage imageNamed:#"More.png"];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:tab1];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:tab2];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:tab3];
UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:tab4];
myAppDelegate.myTabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nil];
[myAppDelegate.rootNav pushViewController:myAppDelegate.myTabBarController animated:YES];
and then later on to push your vc to tab 4 you should do
Profile_ViewController *vc = [[Profile_ViewController alloc] initWithNibName:#"Profile_ViewController" bundle:[NSBundle mainBundle]];
[nav4 pushViewController:vc animated:YES];
here is the Solution related to your query which might help you
You might have kept your architecture as follows
Navigation Controller ---> Tab bar Controller --> 4 child View Controllers
You need to do this
Tab bar Controller --> 4 Child controllers(Each of these 4 child View Controller having its own Navigation Controller).

MMDrawerController with UITabBarController showing a black gap on top of the center view

I am opening a UITabBarController with MMDrawerController on the left side.
So from my delegate didFinishLaunchingWithOptions I am making a MMDrawerController with centerNav is a TabBarController and leftNav is a ViewController with Navigation controller. So as expected the tabbar is coming with the mmdrawer but there is a black gap on top of the LandingTabBarController.If I open the LandingTabBarController directly then it is coming from the top. Whats going wrong?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LandingTabBarController *landingTabBarController = [[LandingTabBarController alloc]initWithManagedObjectContext:self.managedObjectContext];
UIViewController * drawerNavController = [storyboard instantiateViewControllerWithIdentifier:#"DrawerViewController"];
UINavigationController *centerNav = [[UINavigationController alloc]initWithRootViewController:landingTabBarController];
UINavigationController *leftNav = [[UINavigationController alloc]initWithRootViewController:drawerNavController];
centerNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
leftNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
_drawerController = [[MMDrawerController alloc] initWithCenterViewController:centerNav leftDrawerViewController:leftNav];
_drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
_drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModePanningCenterView;
_drawerController.showsShadow = NO;
//[_drawerController setDrawerVisualStateBlock:[MMDrawerVisualState slideVisualStateBlock]];
[_drawerController setDrawerVisualStateBlock:MMDrawerSideNone];
self.window.rootViewController = _drawerController;
[self.window makeKeyAndVisible];
return YES;
}
This is my LandingTabBarController viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
UIViewController *firstViewController = [[UIViewController alloc]init];
[firstViewController.view setBackgroundColor:[UIColor redColor]];
UIViewController *secondViewController = [[UIViewController alloc]init];
[secondViewController.view setBackgroundColor:[UIColor greenColor]];
[tabViewControllers addObject:firstViewController];
[tabViewControllers addObject:secondViewController];
[self setViewControllers:tabViewControllers];
firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"one" image:[UIImage imageNamed:#"icon_feed"] tag:1];
secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"two" image:[UIImage imageNamed:#"icon_apps"] tag:2];
}
Screenshot of the debugger

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

PushView on PresentViewController iOS

I want to Push my viewController on PresentViewController
When i click on button initially i am loading PresentViewController, here is my code.
- (IBAction)JoinClicked:(id)sender{
JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:#"JoinWithViewController" bundle:nil];
[self.view.window.rootViewController presentViewController:detail_view_controller
animated:YES
completion:NULL];
}
this works fine, but when i click on button which is there on PresentViewController on click i want to Push my view, but in does not pushes.
Please help, Thanks in advance.
JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:#"JoinWithViewController" bundle:nil];
[self.navigationController pushViewController:detail_view_controller animated:YES];
Try like this to push viewController. If you use TabBar do like this in AppDelegate. If you create TabBar Drag and drop means leave that. Create TabBar Programatically like below.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITabBarController *tabController = [[UITabBarController alloc]init];
self.viewController = [[CalculatorViewController alloc] initWithNibName:#"CalculatorViewController" bundle:nil];
UITabBarItem *tab_item1 = [[UITabBarItem alloc]init];
//tab_item1.title = #"Calculator";
tab_item1.image = [UIImage imageNamed:#"Calculator.png"];
[self.viewController setTabBarItem:tab_item1];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:self.viewController];
ShowPhoneViewController *phone = [[ShowPhoneViewController alloc]init];
UITabBarItem *tab_item2 = [[UITabBarItem alloc]init];
tab_item2.title = #"Latest Mobiles";
tab_item2.image = [UIImage imageNamed:#"Mobile.png"];
[phone setTabBarItem:tab_item2];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:phone];
CurrencyConvertorViewController *currency = [[CurrencyConvertorViewController alloc]init];
UITabBarItem *tab_item3 = [[UITabBarItem alloc]init];
tab_item3.title = #"Units Convertor";
tab_item3.image = [UIImage imageNamed:#"Dollar.png"];
[currency setTabBarItem:tab_item3];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:currency];
SettingsPageViewController *setting = [[SettingsPageViewController alloc]init];
UITabBarItem *tab_item4 = [[UITabBarItem alloc]init];
tab_item4.title = #"Settings";
tab_item4.image = [UIImage imageNamed:#"Settings.png"];
[setting setTabBarItem:tab_item4];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:setting];
tabController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nav3, nav4, nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
return YES;
}
I hope you got now
pushviewcontroller is the feature of UINavigationController where you can push one viewcontroller on another vierw controller. Here you are using a single viewcontroller as a rootviewcontroller so either you must change your rootviewcontroller to UINavigationcontroller or you can use "addSubview method" to add new viewController on the current viewcontroller.
but the better option is to add uinavigationcontroller as a rootviewcontroller.
AppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FirstViewController *first = [[FirstViewController alloc]
initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navController =[[UINavigationController alloc]
initWithRootViewController:first];
[self.window setRootViewController:navController];
}
Now when as you want to switch from FirstViewController to SecondViewController on button clicked than you have to use pushviewcontroller
FirstViewController.h
-(void) nextBtnPressed {
SecondViewController *second = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:second animated:TRUE];
}

Resources