I am making an application which will have 3 pages
Login Page - first page after the app loads
My First Page- when user successfully logs in then he comes into this page.This page
contains a UITabBar with two UITabBarItems. The first one is connected to
My firstPage
and the other one to My Second Page.
My Second Page - this is another UIViewController.
I have made the login page but I am unable to find the solution to UITabBar adding in My First Page
Please help me out
Define
AppDelegate.h
#property (strong, nonatomic) UITabBarController *tabBarController;
in AppDelegate.m
didFinishLaunchingWithOptions
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
self.tabBarController.selectedIndex=0;
self.tabBarController.delegate=self;
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
return YES;
}
now when you get success login write below code in that method
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
delegate.tabBarController = [[UITabBarController alloc] init];
delegate.tabBarController.selectedIndex = 0;
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:delegate.tabBarController animated:YES];
Try this out,
suppose this is LoginViewController.m
-(IBAction)loginButtonClicked:(id)sender
{
[self createTabBarView];
}
//Create TabBar View here
-(void)createTabBarView
{
NSMutableArray *tabItems = [NSMutableArray array];
UIViewController *firstViewController = [[FirstViewController alloc] init];;
firstViewController = #"First View";
firstViewController = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
[tabItems addObject:firstViewController];
UIViewController *secondViewController = [[SecondViewController alloc] init];;
secondViewController = #"Second View";
secondViewController = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1];
[tabItems addObject:secondViewController];
self.tabBarController = [[UITabBarController alloc]init];
self.tabBarController.viewControllers = tabItems;
self.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:tabBarController animated:YES];
}
Thanks,
Nikhil.
Are you using interface builder?
From my point of view, I'd rather like to use programmatic way to implement it.
//In the appDidFinishLaunch method
BOOL loggedIn = [[NSUserDefault standDefault]boolForKey:"userlogin"];
if(loggedIn)
{
//Setup your UITabbarViewController
}
else
{
//Setup your loginView Controller
}
After login in LogInViewController
- (void)didLogin
{
YourAppDelegate *delegate = [UIApplication shareApplication].delegate;
[delegate.window addSubView:self.tabBarViewController.view];
[delegate.window removeSubView:self.view]
//Other Config
}
You should create Tabbar at place where you can identify that login is successfully done. This method should be part of your loginViewController.
Create a function like below to create a Tabbar and present it over loginController.
-(void) createTabBarController
{
UITabBarController *tabBar = [[UITabBarController alloc]init];
UIViewController *firstViewController = [[[UIViewController alloc] init]autorelease];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
firstNavController.tabBarItem.title=#"First Controller";
firstNavController.tabBarItem.image = [UIImage imageNamed:#"first.png"];
UIViewController *secondViewController = [[[UIViewController alloc] init]autorelease];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];
secondNavController.tabBarItem.title=#"First Controller";
secondNavController.tabBarItem.image = [UIImage imageNamed:#"first.png"];
[tabBar setViewControllers:[NSArray arrayWithObject:firstNavController,secondNavController,nil]];
[firstNavController release];
[secondNavController release];
[self presentModalViewController: tabBar];
[tabBar release];
}
Related
I have an activation page in my app, which is mandatory for every user to activate the app.
Once the app is activated, the user moves to the tabbed bar view.
I have created a tabbed bar application, where from my activationView on click of button I am trying to call the tab bar, I am getting an entire black screen.
- (IBAction)moveToActivateView:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = [[UITabBarController alloc]init];
[self.view addSubview:tabBarController.view];
appDelegate.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController.viewControllers = #[viewController1, viewController2];
appDelegate.window.rootViewController = self.tabBarController;
[appDelegate.window makeKeyAndVisible];}
Hey make your tabBarController's property in appDelegate and assign all ViewController there then call your tabBarController from yourViewController
in AppDelegate.h
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (retain, nonatomic) IBOutlet UITabBarController *tabBarController;
in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self makeTabBar];
[self addInitialVIew];
[self.window makeKeyAndVisible];
return YES;
}
-(void)makeTabBar
{
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
FirstViewController *firstVC =[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC];
firstNC.tabBarItem.title=#"Profile";
firstVC.tabBarController.tabBar.tag = 0;
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *SecondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC];
SecondNavController.tabBarItem.title = #"Search";
secondVC.tabBarController.tabBar.tag = 1;
NSArray *viewControllers =[[NSArray alloc]initWithObjects:firstNC,SecondNavController, nil];
[tabBarController setViewControllers:viewControllers animated:NO];
}
-(void) addInitialVIew
{
InitialViewController *initialViewController = [[InitialViewController alloc]initWithNibName:#"InitialViewController" bundle:nil];
navigationController = [[UINavigationController alloc]initWithRootViewController:ViewController];
[self.window addSubview:navigationController.view];
}
Now in InitialViewController you can add yourTabBar and remove InitialViewController
- (IBAction)switchToTabBarBtnPress:(id)sender
{
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabBarController]view]];
[[appdelegte tabBarController]setSelectedIndex:0];
}
And Follow my answer
Answer
You want to create dynamic tabbar application to resolve your problem. So you just create the view based application. in the view controller viewdidload method put these code
tabbar1 = [[UITabBarController alloc] init];
artist_tab_obj = [[artist_tab alloc] initWithNibName:#"artist_tab" bundle:nil];
UINavigationController *tabItem1 = [[[UINavigationController alloc] initWithRootViewController: artist_tab_obj] autorelease];
tabItem1.title=#"Artist";
tabItem1.tabBarItem.image=[UIImage imageNamed:#"Icon1.png"];
music_tab_obj = [[music_tab alloc] initWithNibName:#"music_tab" bundle:nil];
UINavigationController *tabItem2 = [[[UINavigationController alloc] initWithRootViewController: music_tab_obj] autorelease];
tabItem2.title=#"Music";
tabItem2.tabBarItem.image=[UIImage imageNamed:#"Icon2.png"];
shout_tab_obj = [[shout_tab alloc] initWithNibName:#"shout_tab" bundle:nil];
UINavigationController *tabItem3 = [[[UINavigationController alloc] initWithRootViewController: shout_tab_obj] autorelease];
tabItem3.title=#"Shout";
tabItem3.tabBarItem.image=[UIImage imageNamed:#"Icon3.png"];
schedule_tab_obj = [[schedule_tab alloc] initWithNibName:#"schedule_tab" bundle:nil];
UINavigationController *tabItem4 = [[[UINavigationController alloc] initWithRootViewController: schedule_tab_obj] autorelease];
tabItem4.title=#"Schedule";
tabItem4.tabBarItem.image=[UIImage imageNamed:#"Icon4.png"];
follow_tab_obj = [[follow_tab alloc] initWithNibName:#"follow_tab" bundle:nil];
UINavigationController *tabItem5 = [[[UINavigationController alloc] initWithRootViewController: follow_tab_obj] autorelease];
tabItem5.title=#"Follower";
tabItem5.tabBarItem.image=[UIImage imageNamed:#"Icon5.png"];
tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1, tabItem2,tabItem3,tabItem4,tabItem5,nil];
In user acceptation is yes button action call this code.
[self.view insertSubview:tabbar1.view belowSubview: artist_tab_obj.view];
tabbar1.selectedIndex=1;
[self presentModalViewController:tabbar1 animated:YES];
Did you realize that your local varialbe tabBarController is not identical but sort of hides the property self.tabBarController? You create a new UITabBarCotnroller and assign it to your local variable tabBarController, which is accessible from this method only. Then you manipulate (add newly created view controllers) etc to self.tabBarController. Self.tabBarController could easily be nil here or anything else. But it is not the very UITabBarController that you just created.
And it is self.tabBarController (so probably nil) what you assign to the window as rootViewController.
You do add the tabBarController's view as subview to self.view. Besides that I do not know what self acutally is, I don't think is it really nessessary to manually add the tabBar's view as subview. Setting the root view controller (properly) should be enough
In a UITabBarController, upon selecting a tab, I want that tab's UIViewController to change (assign a new viewcontroller). I'm trying this-
NSMutableArray *tabBarViewControllers = [myUITabBarController.viewControllers mutableCopy];
[tabbarViewControllers replaceObjectAtIndex:0 withObject:[[myViewcontroller1 alloc] init]];
[myUITabBarController setViewControllers:tabbarViewControllers];
But it gives error. How to assign a new UIViewController and refresh instantly?
This is based on femina's answer but doesn't require you to build the whole array of view controllers, it just lets you replace an existing view controller with another. In my case I wanted to swap in a different xib file for the iPhone 5 screen:
if ([[UIScreen mainScreen] bounds].size.height == 568) {
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
Tracking *tracking568h = [[Tracking alloc] initWithNibName:#"Tracking-568h" bundle:nil];
tracking568h.title = [[viewControllers objectAtIndex:0] title];
tracking568h.tabBarItem = [[viewControllers objectAtIndex:0] tabBarItem];
[viewControllers replaceObjectAtIndex:0 withObject:tracking568h];
[tracking568h release];
[self.tabBarController setViewControllers:viewControllers animated:FALSE];
}
This changes the view controller for the first tab, keeping the same tab icon and label.
Please see this code , it offers 2 tabbar's with navigation.
In the AppDelegate.h please declare
UINavigationController *nav1;
UINavigationController *nav2;
UITabBarController *tab;
And in the Appdelegate.m , in the didFinishLaunchingWithOptions please add:-
tab = [[UITabBarController alloc]init];
ViewController *view1 = [[ViewController alloc]init];
nav1= [[UINavigationController alloc]initWithRootViewController:view1];
UITabBarItem *tab1 = [[UITabBarItem alloc]initWithTitle:#"Add" image:[UIImage imageNamed:#"Plus.png"] tag:1];
view1.title = #"Add";
[view1 setTabBarItem:tab1];
SettingsViewController *view2 = [[SettingsViewController alloc]init];
nav2= [[UINavigationController alloc]initWithRootViewController:view2];
UITabBarItem *tab2 = [[UITabBarItem alloc]initWithTitle:#"Setting" image:[UIImage imageNamed:#"settings.png"] tag:2];
view2.title = #"Setting";
[view2 setTabBarItem:tab2];
tab.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nil];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = tab;
Also check this link for further implementation ... Hope this helps :)
UItabBar changing View Controllers
In the AppDelegate.h
UIViewController *vc1;
UIViewController *vc2;
UIViewController *vc3;
in the Appdelegate.m
in the didFinishLaunchingWithOptions
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
vc1 = [[UIViewController alloc] init];
vc1.title = #"A";
[listOfViewControllers addObject:vc1];
vc2 = [[UIViewController alloc] init];
vc2.title = #"B";
[listOfViewControllers addObject:vc2];
vc3 = [[UIViewController alloc] init];
vc3.title = #"C";
[listOfViewControllers addObject:vc3];
[self.tabBarController setViewControllers:listOfViewControllers
animated:YES];
Is there any way to show the UISplitViewController on button click ?
Please use following code..
#interface ...
-(IBAction)Btn_OpenSplitView:(id)sender;
#end
#implementation ...
-(IBAction)Btn_OpenSplitView:(id)sender {
UIViewController* secondVC = [[UIViewController alloc] initWithNibName:#"ContentView"
bundle:nil];
UIViewController* firstVC = [[UIViewController alloc] initWithNibName:#"MenuView"
bundle:nil
withContentViewController:secondVC];
UISplitViewController* splitVC = [[UISplitViewController alloc] init];
splitVC.viewControllers = [NSArray arrayWithObjects:firstVC, secondVC, nil];
[self addSubview:splitVC.view];
}
#end
I have a UIViewController with two UITableView's in it. When i select a row in the first UITableView it has to push the same UIViewController which doesnt happen.
In UIViewController.h i have,
FirstTableViewController *firstController;
SecondTableViewController *secondController;
In UIViewController.m i have,
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if (firstController == nil) {
firstController = [[FirstTableViewController alloc] init];
}
if (secondController == nil) {
secondController = [[SecondTableViewController alloc] init];
}
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstController];
firstController.product = self.product;
[firstTable setDataSource:firstController];
[firstTable setDelegate:firstController];
firstNavigationController.view = firstController.tableView;
}
In FirstTableViewController.m, didSelectRowAtIndexPath i have,
[self.searchDisplayController setActive:YES animated:YES];
UIViewController *controller = [[UIViewController alloc]initWithNibName:#"UIViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.product = prod;
NSLog(#"Navigation controller is %#",self.navigationController); //not null
[[self navigationController]pushViewController:controller animated:YES];
Please help.
EDIT #1: UIViewController is called from the FlipsideViewController of the UtilityApp.
Add a property to your AppDelegate UINavigationController *nav
add this line to AppDelegate.m in application didFinishLaunching method
navigationControl = [[UINavigationController alloc] initWithRootViewController:yourFirst ViewController];
Go to yourFirstViewController, add an UINavigationController *nav property and add these lines to viewDidLoad method
AppDelegate *app = [[UIApplication sharedApplication] delegate];
self.nav = app.nav;
Use this line to push any viewController
[self.nav pushViewController:controller animated:YES];
If you want to show a view controller modally, you should use presentViewController:animated:completion: and not pushViewController:animated:.
[self presentViewController:controller animated:YES completion:nil];
Use this Code in "didSelectRowAtIndexPath"
UIViewController *controller = [[UIViewController alloc]initWithNibName:#"UIViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[viewController release];
LoginViewSimpleController *loginViewSimple = [[LoginViewSimpleController alloc]init];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginViewSimple];
[self setNavigationController:navController];
Have you identify both tableviews ? Can you Post log status here ?
I have a TabBar with ViewController in it. I do this in my AppDelegate. So I have one UINavigationController
test1ViewController = [[Test1ViewController alloc] init];
test2ViewController = [[Test2ViewController alloc] init];
test3ViewController = [[Test3ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: test2ViewController];
NSArray* controllers = [NSArray arrayWithObjects: test1ViewController, navigationController, test3ViewController, nil];
[self.tabBarController setViewControllers:controllers animated:YES];
[navigationController release];
Now I have the problem with this line of source code:
[(Test2ViewController *)[appDelegate.myTabBarController selectedViewController] methodName:arg1 withTag:arg2];
Here there will be a SIGBRT, because the selectedViewController is in this case an "UINavigationController". But I want to call a method of the "Test2ViewController". How could I do this?
Normally I also do this:
if([[appDelegate.myTabBarController selectedViewController] isKindOfClass:[Test2ViewController class]]) { ... }
But this also fail because it is a UINavigationController. How to fix that? Does anyone know?
Thanks a lot in advance & Best Regards.
Try the following:
UINavigationController *navController = (UINavigationController *) [appDelegate.myTabBarController selectedViewController];
Test2ViewController *viewController = (Test2ViewController *) [[navController viewControllers] objectAtIndex: 0];
[viewController methodName:arg1 withTag:arg2];