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
Related
i have added tab bar in app delegate. When i pushed the view from one of my view te tab bar got removed. I want that tab bar on pushed view also.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
AudioViewController * audioViewController = [[AudioViewController alloc] initWithNibName:#"AudioViewController" bundle:nil];
audioViewController.title = #"audio";
audioViewController.tabBarItem.image=[UIImage imageNamed:#"audio 30x30.png"];
ViewController *videoViewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
videoViewController.title = #"video";
videoViewController.tabBarItem.image=[UIImage imageNamed:#"video 30x30.png"];
ViewController *aboutViewController = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
aboutViewController.title = #"about";
aboutViewController.tabBarItem.image=[UIImage imageNamed:#"about1_iPhone.png"];
ViewController *infoViewController = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
infoViewController.title = #"info";
infoViewController.tabBarItem.image=[UIImage imageNamed:#"info 30x30.png"];
PlaylistViewController *PlaylistViewControllerObj = [[PlaylistViewController alloc]initWithNibName:#"PlaylistViewController" bundle:nil];
PlaylistViewControllerObj.title = #"Playlist";
PlaylistViewControllerObj.tabBarItem.image=[UIImage imageNamed:#"ko.png"];
NSArray *viewControllerArray = [NSArray arrayWithObjects:audioViewController,videoViewController,aboutViewController,infoViewController,PlaylistViewControllerObj,nil];
UITabBarController * myTabbarController = [[UITabBarController alloc] init];
[myTabbarController setViewControllers:viewControllerArray];
navigationControllerObj = [[UINavigationController alloc] initWithRootViewController:myTabbarController];
[navigationControllerObj setNavigationBarHidden:YES animated:NO];
self.window.rootViewController =navigationControllerObj;
}
UIViewController *viewController1, *viewController2, *viewController3 , *viewController4 , *viewController5 ;
viewController1 = [[Homeviewcontroller alloc] initWithNibName:nil bundle:nil];
viewController2 = [[NearbyViewController alloc] initWithNibName:nil bundle:nil];
viewController3 = [[SearchViewController alloc] initWithNibName:nil bundle:nil];
viewController4 = [[FavoritiesViewController alloc] initWithNibName:nil bundle:nil];
viewController5 = [[MoreViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *homeNavi=[[UINavigationController alloc]initWithRootViewController:viewController1];
UINavigationController *SearchNavi=[[UINavigationController alloc]initWithRootViewController:viewController3];
UINavigationController *NearbyNavi=[[UINavigationController alloc]initWithRootViewController:viewController2];
UINavigationController *FavNavi=[[UINavigationController alloc]initWithRootViewController:viewController4];
UINavigationController *MoreNavi=[[UINavigationController alloc]initWithRootViewController:viewController5];
TabbarController = [[UITabBarController alloc] init];
TabbarController.viewControllers = [NSArray arrayWithObjects:homeNavi,SearchNavi,NearbyNavi,FavNavi,MoreNavi, nil];
[self presentModalViewController:TabbarController animated:YES];
Check value of this property in the UIViewController you are pushing
#property(nonatomic) BOOL hidesBottomBarWhenPushed
Try this sample code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
AudioViewController * audioViewController = [[AudioViewController alloc] initWithNibName:#"AudioViewController" bundle:nil];
audioViewController.title = #"audio";
audioViewController.tabBarItem.image=[UIImage imageNamed:#"audio 30x30.png"];
UINavigationController *navaudioViewController = [[UINavigationController alloc] initWithRootViewController:audioViewController];
PlaylistViewController *PlaylistViewControllerObj = [[PlaylistViewController alloc]initWithNibName:#"PlaylistViewController" bundle:nil];
PlaylistViewControllerObj.title = #"Playlist";
PlaylistViewControllerObj.tabBarItem.image=[UIImage imageNamed:#"ko.png"];
UINavigationController *navPlaylistView = [[UINavigationController alloc] initWithRootViewController:PlaylistViewControllerObj];
UITabBarController * myTabbarController = [[UITabBarController alloc] init];
myTabbarController.viewControllers = #[navHomeController,navPlaylistView];
self.window.rootViewController = myTabbarController;
[self.window makeKeyAndVisible];
return YES;
}
Use This Code
call this method , where to present UITabBar
In .h,
#property (strong, nonatomic) UINavigationController *navigation;
#property(nonatomic, strong) UITabBarController *tabbarcontroller;
In .m,
-(void)loadtabview
{
self.tabbarcontroller = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
self.firstViewController = [[FirstViewController alloc]initWithNibName:#"firstViewController" bundle:nil];
navigation = [[UINavigationController alloc] initWithRootViewController:self.firstViewController];
self.viewController.navigationItem.title=#"First";
[localControllersArray addObject:navigation];
self.secondViewController = [[secondViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
navigation = [[UINavigationController alloc] initWithRootViewController:secondViewController];
self.secondViewController.navigationItem.title=#"second";
[localControllersArray addObject:navigation];
self.ThirdViewController = [[Third ViewController alloc]initWithNibName:#"Third ViewController" bundle:nil];
navigation = [[UINavigationController alloc] initWithRootViewController:ThirdViewController];
self.secondViewController.navigationItem.title=#"Third";
[localControllersArray addObject:navigation];
tabbarcontroller.viewControllers = localControllersArray;
self.tabbarcontroller.delegate = self;
[self.tabbarcontroller setSelectedIndex:0];
[self.window addSubview:tabbarcontroller.view];
}
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];
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];
}
I need to add a UItabbar programmatically to a UIView. This is the code below, the app starts from a RootViewTableController, then gets pushed to this view, but the tabs don't work maybe it's something in MainWindow.xib that's the problem?:
// TabBarController.h
#interface TabBarController : UITabBarController
#end
// TabBarController.m
#import "TabBarController.h"
#import "FirstV.h"
#implementation TabBarController
-(void)viewDidLoad {
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
UIViewController *vc;
vc = [[UIViewController alloc] init];
vc.title = #"A";
[listOfViewControllers addObject:vc];
[vc release];
vc = [[UIViewController alloc] init];
vc.title = #"B";
[listOfViewControllers addObject:vc];
[vc release];
[self.tabBarController setViewControllers:listOfViewControllers animated:YES];
[super viewDidLoad];
}
#end
Screenshot here:
As you can see no tabs at the bottom, should I drag and drop a UItabbar element into the view?
Try putting your [super viewDidLoad]; ABOVE your code where it should be.
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];