can we use UITab bar controller in view base application thank you
Yes you can.
you can look at the "TheElements" example that apple provides.
you can find it here:
https://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html
look at the app delegate.
its very strait forward example.
for you request i tried to make a simple example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupPortraitUserInterface];
return YES;
}
- (UINavigationController *)AchievementsControllerWrappingViewController:(NSInteger*)tabIndex{
switch(tabIndex){
case 0:
FirstViewController *theViewController;
theViewController = [[FirstViewController alloc] init];
break;
case 1:
SecondViewController *theViewController;
theViewController = [[SecondViewController alloc] init];
break;
}
UINavigationController *theNavigationController;
theNavigationController = [[UINavigationController alloc] initWithRootViewController:theViewController];
[theViewController release];
return theNavigationController;
}
- (void)setupPortraitUserInterface {
UINavigationController *localNavigationController;
UIWindow *localWindow;
localWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = localWindow;
[localWindow release];
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
for(int i=0;i<2;i++){
localNavigationController = [self AchievementsControllerWrappingViewController:i];
[localViewControllersArray addObject:localNavigationController];
[localNavigationController release];
}
tabBarController.viewControllers = localViewControllersArray;
[localViewControllersArray release];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
i am not next to xCode and i did it with text edit, so please check it when you use it.
shani
Yes we can..
For that you have to create UITabBarViewController and its Object then refrence it with you Application..like :
in your AppDelegate.h
#interface youAppDelegate.h : UIApplicationDelegate {
UIWindow *window;
YourViewController *viewController;
// Declare Your TabBarController Here
UITabBarController *tabBar;
}
#property (nonautomic, retain) IBOutlet UIWindow *window;
#property (nonautomic, retain) IBOutlet TabBarViewController *tabBar;
#end
in you implementation file's ApplicationDidFinish Launching add the following Code
viewController = [[YourViewController alloc] init];
[viewController addSubView:tabBar];
[self.window addSubView:viewController];
In interface builder you have to add the TabBarController to your MainWindow and relate it with the IBOutLet. Give what ever view you want to display in tabBar.
enjoy..
Related
I implemented the SplitView in my app as follow.
AppDelegate.h
#property (nonatomic, strong) IBOutlet UISplitViewController *splitViewController;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LeftsideViewController *masterViewController = [[LeftsideViewController alloc] initWithNibName:#"LeftsideViewController" bundle:nil] ;
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController] ;
HomeViewController *detailViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController_iPad" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
masterViewController.homeViewController = detailViewController;
self.splitViewController = [[UISplitViewController alloc] init] ;
[self.splitViewController.view setBackgroundColor:[UIColor blackColor]];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers=[NSArray arrayWithObjects:masterNavigationController,detailNavigationController,nil];
self.window.rootViewController = self.splitViewController;
NSLog(#"VIEWs %#",[self.window subviews]);
[self.window makeKeyAndVisible];
return YES;
}
Problem
It is working fine in ios 6.
But in ios 7 it shows the some view which i did not included.
See my following image & the extra view is in Pink Color.
float systemversion=[[[UIDevice currentDevice]systemVersion]floatValue];
if(systemversion>=7.0f)
{
self.edgesForExtendedLayout=UIRectEdgeNone;
}
Try this code
I have added Two Tabs in my app, to load two view controller using these tabs
Tab1 : Home
Tab2 : Favourite
so I have written below code to achieve this
In app Delegate
AppDelegate.h
#class ViewController,FavViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (strong, nonatomic) FavViewController *favViewController;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
AppDelegate.m
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController;
#synthesize favViewController;
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
baseTabBarController = [[UITabBarController alloc]init];
baseTabBarController.delegate=self;
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *homeView = [[UINavigationController alloc]initWithRootViewController:viewController];
favViewController = [[FavViewController alloc] initWithNibName:#"FavViewController" bundle:nil];
favViewController.title = #"My Favourite";
UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:favViewController];
NSArray *controllers = [NSArray arrayWithObjects:homeView,favouriteView, nil];
baseTabBarController.viewControllers = controllers;
[self.window addSubview:baseTabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)selectedViewController
{
if (tabBarController.selectedIndex == 0) {
}else if (tabBarController.selectedIndex == 1) {
[(FavViewController *)[(UINavigationController*)selectedViewController topViewController] getData];
}else if (tabBarController.selectedIndex == 2) {
NSLog(#"2");
}
}
and here is the Result Screen I am getting
So Where I am having trouble in this..
If I switch to the next screen my First tab doesn't load First Screen (Home screen) instead just stay on current screen.
Let me try with example
There are four screens in my app let say A, B, C, D
I have added Tabs for A and C screens,those are available in whole app
(All screen).
Now if I'll start app and go A ->B->C -> D and press on Home Tab (A)
it doesn't load Screen A, instead just stay on Current screen
but good thing is if i do same process with another tab C (My
Favourite) it loads correct screen.
Edit: I have implemented didSelectViewController method as #sanjit suggested me but in that I not able distinguish, which tab is tapped this time?
I would be really appreciate!! if someone can point me on right direction
Use Tabbar delegate and using parameter instance of uiviewcontroller call poptorootviewcontroller method. Hope it may work for you. Try the following code
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)tabBarController.selectedViewController;
[navController popToRootViewControllerAnimated:YES];
}
replace the code
[viewController setTabBarItem:tab1];
[favViewController setTabBarItem:tab2];
with
[[tabbarController tabBar] setItems:[NSArray arrayWithObjects:tab1,tab2, nil]];
that should resolve.
Viewcontroller.h
#property(nonatomic, retain) UITabBarItem *item1;
#property(nonatomic, retain) UITabBarItem *item2;
#property(nonatomic, retain) UITabBarItem *item3;
#property(nonatomic, retain) UITabBarItem *item4;
#property(nonatomic, retain) IBOutlet UITabBar *tabBar;
Viewcontroller.m
#synthesize item1,item2,item3,item4,tabBar;
- (void)viewDidLoad
{
item1 = [[UITabBarItem alloc] initWithTitle:#"Home" image:[UIImage imageNamed:#"home.png"] tag:0];
item2 = [[UITabBarItem alloc] initWithTitle:#"My Lists" image:[UIImage imageNamed:#"mylist"] tag:1];
item3 = [[UITabBarItem alloc] initWithTitle:#"Search" image:[UIImage imageNamed:#"search.png"] tag:2];
item4 = [[UITabBarItem alloc] initWithTitle:#"Settings" image:[UIImage imageNamed:#"setting.png"] tag:3];
NSArray *items = [NSArray arrayWithObjects:item1,item2,item3,item4, nil];
[tabBar setItems:items animated:NO];
[tabBar setSelectedItem:[tabBar.items objectAtIndex:2]];
tabBar.delegate=self;
[self.view addSubview:tabBar
];
}
/****Function Call******/
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item.tag == 0)
{
Newsfeeds *obj = [[Newsfeeds alloc]initWithNibName:#"Newsfeeds" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
else if(item.tag == 1)
{
MyLists *obj = [[MyLists alloc]initWithNibName:#"MyLists" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
else if(item.tag == 3)
{
Settings *obj = [[Settings alloc]initWithNibName:#"Settings" bundle:Nil];
[self.navigationController pushViewController:obj animated:NO];
}
}
II wanted to add a UIBarButton to my UINavigationController. I did that with the help of following code,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addInfoButton = [[UIBarButtonItem alloc] initWithTitle:#"Add Info" style:UIBarButtonItemStylePlain target:self action:#selector(addCustomerInfo)];
self.navigationItem.rightBarButtonItem = addInfoButton;
}
-(void) addCustomerInfo
{
AddInfoViewController *addVC = [[AddInfoViewController alloc] initWithNibName:#"AddInfoViewController" bundle:nil];
[addVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:addVC animated:YES];
}
Should I declare "-(void) addCustomerInfo" in .h file? I already tried that but no luck.
The code still throws the exeption,
2012-08-06 04:16:22.200 TableView[5698:f803] -[RootViewController addCustomerInfo]: unrecognized selector sent to instance 0x6c662b0
2012-08-06 04:16:22.202 TableView[5698:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController addCustomerInfo]: unrecognized selector sent to instance 0x6c662b0'
The code for your class is correct. You need to change your App Delegate
I suggest creating a property in your App Delegate to store your navigation controller -#property (strong, nonatomic) UINavigationController *navController; - Don't forget to synthesize it.
Then, when you create your Navigation controller, set it to your property - self.navController = [[UINavigationController alloc] init];
This will guarantee your NavigationController is properly retained and that it can be accessed correctly by other classes in your application.
Below some sample code that might make it clearer:
First the AppDelegate header file:
//AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navController;
#end
And the implementation file:
// AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.navController = [[UINavigationController alloc] init];
UIViewController *myVC= [[myVC alloc] init];
[self.navController pushViewController:myVC animated:NO];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Check out this tutorial:
http://www.innovatelabs.in/2010/03/implementing-uibarbuttonitem/
I'm not so fluent with iOS, but usually when you use a selector, your add : to it #selector(addCustomInfo:)
and then the function would look like this:
-(void) addCustomInfo:(UIBarButtonItem *)myButton {
NSLog(#"YOU CLICKED ME!");
}
In my app delegate interface I have:
#interface pivcalc1AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UITabBarController *RootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *Window;
#property (nonatomic, retain) IBOutlet UITabBarController *RootController;
in implementation, I have:
#synthesize Window;
#synthesize RootController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
[Window addSubview:RootController.view];
// Override point for customization after application launch.
[self.Window makeKeyAndVisible];
return YES;
}
In my main xib window I have a tab bar controller which is connected to app delegate as rootController.
When I run the program, the window shows but the tab bar view does not get loaded.
Appreciate any help. Thanks.
Are you have a subViewController in RootController in xib? if you're not, It's code following.
UITabBarController should be have a subViewController.
UINavigationController *localNavigationController;
NSMutableArray *localViewControllerArray = [[NSMutableArray alloc] initWithCapacity:2];
SubViewController *subviewController0 = [[SubViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:subviewController0];
[localViewControllerArray addObject:localNavigationController];
[subviewController0 release];
[localNavigationController release];
SubViewController *subviewController1 = [[SubViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:subviewController1];
[localViewControllerArray addObject:localNavigationController];
[subviewController1 release];
[localNavigationController release];
RootController.viewControllers = localViewControllerArray;
[localViewControllerArray release];
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
I have following coding in my Program, in AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
myList = [[List alloc] init];
mySettings = [[Settings alloc] init];
myCriteria = [[Criteria alloc] initWithStyle:UITableViewStyleGrouped];
first = [[UINavigationController alloc] initWithRootViewController:myList];
second = [[UINavigationController alloc] initWithRootViewController:mySettings];
third = [[UINavigationController alloc] initWithRootViewController:myCriteria];
myTabBar = [[UITabBarController alloc] init];
myTabBar.view.frame = CGRectMake(0,20,320,460);
UITabBarItem *first1 = [[UITabBarItem alloc] initWithTitle:#"List" image:[UIImage imageNamed:#"list.png"] tag:0];
UITabBarItem *second2 = [[UITabBarItem alloc] initWithTitle:#"My Profile" image:[UIImage imageNamed:#"settings.png"] tag:1];
UITabBarItem *third3 = [[UITabBarItem alloc] initWithTitle:#"Criteria" image:[UIImage imageNamed:#"Criteria.png"] tag:2];
UINavigationController *localNavigationController = [[UINavigationController alloc] initWithRootViewController:myTabBar];
first.tabBarItem = first1;
second.tabBarItem = second2;
third.tabBarItem = third3;
myControllerArray = [[NSArray alloc] initWithObjects:first, second, third, nil];
[myTabBar setViewControllers:myControllerArray];
[myTabBar setCustomizableViewControllers:myControllerArray];
[self.window addSubview:myTabBar.view];
// Add the view controller's view to the window and display.
// [self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
Where these three are different classes, now I want to use camera and album, so when I write coding as below on a button target, it won't work
- (void) useCamera
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
NSLog(#"If is true");
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
// [self.navigationController pushViewController:imagePicker animated:YES];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
[self.tabBarController setCustomizableViewControllers:[[NSArray alloc]initWithObjects:imagePicker,self,nil]];
[self.tabBarController setViewControllers:[[NSArray alloc]initWithObjects:imagePicker,self,nil]];
//[self presentModalViewController:imagePicker animated:YES]; I tried
//[self.view addSubview:imagePicker.view]; I tried
//self.view=imagePicker.view; I tried
//[myScrollView addSubview:imagePicker]; I tried
//[myView addSubview:imagePicker]; I tried
[imagePicker release];
newMedia = YES;
}
}
I tried all the above ways but they are not working and showing UIImagePickerController,
now what should I do
if anyone in not clear from my question, can ask me again.....
What about [self.view addSubview:imagePicker.cameraOverlayView]
Also, have you tried adding any other view to your self.view? I wonder if the imagePicker is being added, but is not loaded because the view you are adding it to is not being displayed.
Did u try presenting the imagePicker through one of the navigation controllers?
From Reference
Present the user interface by calling the presentModalViewController:animated: method of the currently active view controller, passing your configured image picker controller as the new view controller. On iPad, you can alternatively present the user interface using a popover as described in initWithContentViewController: and “Presenting and Dismissing the Popover” in UIPopoverController Class Reference.
If using GKImagePicker:
[self.view addSubview:self.imagePicker.imagePickerController.view];
You can always do:
let cam = UIImagePickerController()
cam.delegate = self
cam.sourceType = .camera
self.show(cam, sender: nil)
worked for me
Did you try presenting the UIImagePickerController through the tabBarController (UITabBarController inherits from UIViewController)? Assuming that you created a singleton for for your AppDelegate and that the tabBarController is a property of the AppController (for your convenience I added a sample code for the singleton below), the code would look something like this:
AppController.h
#import <UIKit/UIKit.h>
#interface AppController : NSObject <UIApplicationDelegate> {
UITabBarController *tabBarController;
}
// Class methods for convenience
+ (AppController *)sharedAppController;
// *** Properties
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) UITabBarController *tabBarController;
#end
AppController.m
#import "AppController.h"
static AppController *sharedInstance;
#implementation AppController
#synthesize window=_window;
#synthesize tabBarController;
#pragma mark - Initialization
- (id)init
{
// Create the sharedInstance of the application
if (sharedInstance) {
NSLog(#"Error: You are creating a second AppController");
}
[super init];
sharedInstance = self;
return self;
}
+ (AppController *)sharedAppController
{
return sharedInstance;
}
- (void)dealloc
{
[_window release];
[tabBarController release];
[super dealloc];
}
Now in your class, on the useCamera method call the imagePicker by doing this:
- (void)useCamera
{
// YOUR CODE ...
// Place image picker
[[[AppController sharedAppController] tabBarController]
presentModalViewController:imagePicker animated:YES];
// YOUR RELEASE CODE ...
}
Good luck and happy coding!