iOS Tab Bar Interface - objective-c

I have a login screen for a UITabBar application where users are divided into two categories. I want to show a certain tab for one type of user and other tabs for the other type of user. Does anyone have an idea of where to start?

In one of my app i have used this code to set tabbarcontroller in between app
First create login screen without tabbarcontroller
This is just a example modify according your condition
define tabbar controller in your AppDelegate.m
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
self.tabBarController.selectedIndex=0;
self.tabBarController.delegate=self;
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
// return YES;
}
Apply below code where you login and push your controller with tab bar controller
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIViewController *viewController1 = [[GeneralViewController alloc] initWithNibName:#"GeneralViewController" bundle:nil];
UIViewController *viewController2 = [[MiscQuotationController alloc] initWithNibName:#"MiscQuotationController" bundle:nil];
UIViewController *viewController4 = [[QuotationListController alloc] initWithNibName:#"QuotationListController" bundle:nil];
UIViewController *viewController5 = [[ChargesViewController alloc] initWithNibName:#"ChargesViewController" bundle:nil];
UIViewController *viewController7 = [[SalesPartViewController alloc] initWithNibName:#"SalesPartViewController" bundle:nil];
/// tab button title
viewController1.title = #"Basic information";
viewController2.title = #"Misc Quotation";
viewController4.title = #"Quotation Line";
viewController5.title = #"Charges";
viewController7.title = #"Sales Part Stock";
// tab button Images
viewController1.tabBarItem.image = [UIImage imageNamed:#"general.png"];
viewController2.tabBarItem.image = [UIImage imageNamed:#"misle.png"];
viewController4.tabBarItem.image = [UIImage imageNamed:#"history.png"];
viewController5.tabBarItem.image = [UIImage imageNamed:#"charges.png"];
viewController7.tabBarItem.image = [UIImage imageNamed:#"shoebox.png"];
delegate.tabBarController = [[UITabBarController alloc] init];
delegate.tabBarController.selectedIndex = 0;
// Write your condition of user here
if(user == admin){
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController4, viewController5, viewController7, nil];
}
else{
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects: viewController2, viewController4, viewController5, viewController7, nil];
}
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController4, viewController5, viewController7, nil];
delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:delegate.tabBarController animated:YES];

Related

Objective C - Push to a view controller not in tabBarController's tabs programmatically

I have a tab bar controller which contains 3 view controllers, and in 1 of those view controllers, I have a button to present another view controller not in the tab bar controller. The functions in the button run, but the other view controller does not show up. To be more specific, the 3 view controllers in the tab bar is the ProfilePage, EditProfile page and AllUsersProfile page. And in my ProfilePage, I have a logout button to go back to the login screen, which is not part of the tabBar.
Here is my code:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
// last login
NSString * username = [[NSUserDefaults standardUserDefaults] objectForKey:#"SessionKey"];
Users* lastUser =[LocalStorageController loadCustomObjectWithKey:username];
ProfilePage *lastUserProfile = [[ProfilePage alloc] initWithUser:lastUser];
EditProfile *lastUserEdit = [[EditProfile alloc]initWithUser:lastUser];
AllUsersProfile *lastUserViewAll = [[AllUsersProfile alloc]init];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:lastUserProfile,lastUserEdit,lastUserViewAll,nil];
[tabBarController setViewControllers:viewControllers animated:NO];
[tabBarController release];
self.window.rootViewController = tabBarController;
lastUserEdit.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Edit Profile" image:nil selectedImage:nil];
lastUserProfile.tabBarItem =[[UITabBarItem alloc] initWithTitle:#"Home" image:nil selectedImage:nil];
lastUserViewAll.tabBarItem =[[UITabBarItem alloc] initWithTitle:#"View Others" image:nil selectedImage:nil];
And here is the code for logout:
- (void) logOut{
[self dismissViewControllerAnimated:YES completion: nil]; //this does not show the LoginScreen view controller
}
Please check the current code to present a new controller in TabBar controllers.
// You have three UIViewControllers
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
ViewControllerTab1 * tabVC1 = [[ViewControllerTab1 alloc] init];
ViewControllerTab2 * tabVC2 = [[ViewControllerTab2 alloc] init];
ViewControllerTab3 * tabVC3 = [[ViewControllerTab3 alloc] init];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray * viewControllers = [NSArray arrayWithObjects:tabVC1, tabVC2, tabVC3, nil];
[tabBarController setViewControllers:viewControllers animated:NO];
self.window.rootViewController = tabBarController;
tabVC1.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Edit Profile" image:nil selectedImage:nil];
tabVC2.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Home" image:nil selectedImage:nil];
tabVC3.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"View Others" image:nil selectedImage:nil];
}
In ViewControllerTab1, present NewViewController which you want as per your requirement
-(void)presentAnotherVC{
NewViewController * vc = [[NewViewController alloc]init];
[self presentViewController:vc animated:YES completion:nil];
}
In NewViewController, dismiss NewViewController to go back to selected TabBar controller
-(void)dsmissThisVC{
[self dismissViewControllerAnimated:YES completion:nil];
}

Correct way to pushViewController in this case?

I have a custom UITabBarController. On it I have added 3 view controllers adding a navigationController to each one first. It looks like this
ยท customTabBarController.m viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
FirstViewController *firstController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController:firstController];
SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:secondController];
self.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];
[self addLeftButtonImage];
[self addRightButtonImage];
self.selectedIndex = 1;
}
Now, when I am on a view controller and I want to pushViewController, how do I have to call it?

SplitViewController with TabbarController

I am using the split View functionality in my app.I have to put tabbar in rootViewController.
But when I add controllers in tabbar and add them into split view it doesnt split.
It only shows detailViewController.
Here is the code in application did finish launching:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIViewController *viewController2,*viewController3,*viewController4;
rootViewController = [[[CategoryItemsList alloc] init] autorelease];
viewController2 = [[[SearchList alloc] init] autorelease];
viewController3 = [[[FavoritesList alloc] init] autorelease];
viewController4 = [[[Information alloc] init] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects: rootViewController,viewController2,viewController3,viewController4, nil];
splitDetail = [[splitDetailView alloc] initWithNibName:#"splitDetailView" bundle:nil];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:tabBarController];
splitViewController = [[UISplitViewController alloc] init];
// splitViewController.tabBarItem = controller.tabBarItem;
splitViewController.viewControllers = [NSArray arrayWithObjects:nav, splitDetail, nil];
splitViewController.delegate=splitDetail;
rootViewController.splitView=splitDetail;
}
You can refer to this links. I think this is what you are looking out for:
https://github.com/mattgemmell/MGSplitViewController
http://www.raywenderlich.com/1040/ipad-for-iphone-developers-101-uisplitview-tutorial
http://mikebluestein.wordpress.com/2010/04/03/using-a-uisplitviewcontroller-to-create-a-master-detail-ipad-app-with-monotouch/
Once you add the split view then you just need to add the object of the SplitViewController to the TabBarController as one of the viewControllers
Hope this helps.

set array .viewControllers from SplitViewController

I'm trying to use a splitview inside a TabBar. By now I have a SplitView in my first TabBarItem. My problem comes when I try to access to a different DetailView or right view in the SplitViewController I have.
I'm trying to do it inside the didSelectRowAtIndexPath: of my Root (or Master) viewcontroller from the SplitView.
Here's the code, where I try to acces to my TabBarController from an AppDelegate object, and change the viewControllers array of my SplitView only changing the second view controller. I always get this crash error, saying that 2nd instance send is unrecognized: -[SecondViewController viewControllers]: unrecognized selector sent to instance 0x6852460
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//object AppDelegate
AppDelegate *myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//Objecte in Index 0 is my SplitVC
NSArray *barControllers = myDelegate.tabBarController.viewControllers;
if (indexPath.row == 0)
{
SplitVC *temporalSplit = [barControllers objectAtIndex:indexPath.row];
NSArray *mArray = temporalSplit.viewControllers;
FirstViewController *detail = [[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:detail];
temporalSplit.delegate = detail;
temporalSplit.viewControllers = [[NSArray alloc] initWithObjects:[mArray objectAtIndex:0], navigationDetail, nil];
myDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:temporalSplit.viewControllers, [barControllers objectAtIndex:1], nil];
}
else if (indexPath.row == 1)
{
SplitVC *temporalSplit = [barControllers objectAtIndex:indexPath.row];
NSArray *mArray = temporalSplit.viewControllers;
Detail2VC *detail = [[Detail2VC alloc]initWithNibName:#"Detail2VC" bundle:nil];
UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:detail];
temporalSplit.delegate = detail;
temporalSplit.viewControllers = [[NSArray alloc] initWithObjects:[mArray objectAtIndex:0], navigationDetail, nil];
myDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:[barControllers objectAtIndex:0], temporalSplit, nil];
}
[myDelegate release];
}
And my AppDelegate code (that works without problems):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
// Override point for customization after application launch.
NSMutableArray *controllersBar = [[NSMutableArray alloc]init];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
for (int i = 0; i<2; i++)
{
if(i == 0)
{
_firstViewController = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
_masterApp = [[MasterVC alloc]initWithNibName:#"MasterVC" bundle:nil];
_masterApp.firstViewController = _firstViewController;
_firstViewController.mastervc = _masterApp;
UINavigationController *navigationMaster = [[UINavigationController alloc]initWithRootViewController:_masterApp];
UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:_firstViewController];
_splitVC = [[SplitVC alloc] initWithNibName:nil bundle:nil];
//_splitVC.tabBarItem = controller.tabBarItem;
_splitVC.viewControllers = [NSArray arrayWithObjects:navigationMaster, navigationDetail, nil];
_splitVC.delegate = _firstViewController;
[controllersBar addObject:_splitVC];
}
else if (i == 1)
{
[controllersBar addObject:viewController2];
}
}
//self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
_tabBarController.viewControllers = controllersBar;
_tabBarController.selectedIndex = 0;
_tabBarController.delegate = self;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
What am I doing wrong? Any help or suggestion is welcome.
Thanks to all in advance.
Split view controller MUST be root level controllers:
UISplitviewcontroller not as a rootview controller
If not, you can apparently get all sorts of strange things happening.

Title of tab bar items not showing

![I am creating a tab bar controller programmatically:
tabBarController = [[UITabBarController alloc] init];
FirstViewController* vc1 = [[FirstViewController alloc] init];
SecondViewController* vc2 = [[SecondViewController alloc] init];
vc1.title = #"Dallas";//[[NSUserDefaults standardUserDefaults] objectForKey:#"Citynamefrmhome"];
vc1.tabBarItem.image = [UIImage imageNamed:#"Dealss.png"];
vc2.title = #"My Vouchers";
vc2.tabBarItem.image = [UIImage imageNamed:#"nav_voucher_S.png"];
NSArray* controllers = [NSArray arrayWithObjects:vc1,vc2, nil];
tabBarController.viewControllers = controllers;
[self.view addSubview:tabBarController.view];
[vc1 release];
[vc2 release];
if I load it when the app starts, it looks great (look at figure 1):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
autoMagically = [[AutoMagically alloc] initWithNibName:nil bundle:nil];
//self.window.rootViewController = self.dummyView;
[self.window addSubview:autoMagically.view];
[self.window makeKeyAndVisible];
return YES;
}
if I load it when a button is clicked, the way i wanna do it, it looks like figure 2. See how the titles arent showing and the buttons seem to be getting cutoff:
- (void)LoadView
{
AutoMagically *autoMagic = [[AutoMagically alloc]
initWithNibName:nil bundle:nil];
self.autoMagically = autoMagic;
[self.view insertSubview:autoMagic.view atIndex:0];
[autoMagic release];
}
Does anyone know why this is behaving like this?]1
You can call
[[[self.parentViewController.tabBarController.tabBar.items objectAtIndex:/*a number based on what tab it was ex: 2*/]setTitle:#"string"];
in your -viewDidLoad or -awakeFromNib methods. It might be better to use the latter instead of the former.