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];
}
}
Related
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];
Iknow how to add a TabBar with two TabBar buttons on it by Interface Builder. And I link each button to a Navigation Controller.
Now I want to learn how to do everything programmatically. Now I can see a TabBar in simulator but with no buttons on it. Can someone please help me with this. Thanks!
Here's the TabBarAppDelegate.h.
#import <UIKit/UIKit.h>
#interface TabBarAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UITabBarController *tabBarController;
IBOutlet UINavigationController *navigationController1;
IBOutlet UINavigationController *navigationController2;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) UITabBarController *tabBarController;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController1;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController2;
#end
Here's TabBarAppDelegate.m.
#import "TabBarAppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#implementation TabBarAppDelegate
#synthesize window=window;
#synthesize tabBarController;
#synthesize navigationController1;
#synthesize navigationController2;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
tabBarController = [[UITabBarController alloc]init];
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
firstViewController.title = #"First";
[self.navigationController1 pushViewController:firstViewController animated:NO];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.title = #"NavCal";
[self.navigationController2 pushViewController:secondViewController animated:NO];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];
[window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
[firstViewController release];
[secondViewController release];
return YES;
}
- (void)dealloc
{
[tabBarController release];
[window release];
[super dealloc];
}
It looks like you are not initializing the navigation view controllers. See if this would work:
TabBarAppDelegate.h
Remove the properties navigationController1 and navigationController2
TabBarAppDelegate.m
Replace
[self.navigationController1 pushViewController:firstViewController animated:NO];
with
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstViewController];
Replace
[self.navigationController2 pushViewController:secondViewController animated:NO];
with
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:secondViewController];
Release navigationController1 and navigationController2 after adding them to the tabBarController
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!
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..
This is a very important auto rotate issue and easy to reproduce.
My application has a UITabBarController. Each tab is a UINavigationController. Auto rotation is handled with normal calls to shouldAutorotateToInterfaceOrientation and didRotateFromInterfaceOrientation.
The interface rotates normally until I call UIViewController.popViewControllerAnimated and change UITabBarController.selectedIndex.
Steps to reproduce:
Create a demo Tab Bar Application.
Add the following code to the App Delegate .h file:#import <UIKit/UIKit.h>
#interface TestRotationAppDelegate : NSObject {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
// Redefine the interface to cach rotation messages
#interface UITabBarController (TestRotation1AppDelegate)
#end
Add the following code to the App Delegate .m file:#import "TestRotationAppDelegate.h"
#implementation TestRotationAppDelegate
#synthesize window;
#synthesize tabBarController;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
#end
#implementation UITabBarController (TestRotation1AppDelegate)
-(void)viewDidLoad {
[super viewDidLoad];
// Add a third tab and push a view
UIViewController *view1 = [[[UIViewController alloc] init] autorelease];
view1.title = #"Third";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:view1] autorelease];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:self.viewControllers];
[array addObject:nav];
self.viewControllers = array;
// Push view2 inside the third tab
UIViewController *view2 = [[[UIViewController alloc] init] autorelease];
[nav pushViewController:view2 animated:YES];
// Create a button to pop view2
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 50, 220, 38);
[button setTitle:#"Pop this view" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doAction) forControlEvents:UIControlEventTouchUpInside];
[view2.view addSubview:button];
}
-(void) doAction {
// ROTATION PROBLEM BEGINS HERE
// Remove one line of code and the problem doesn't occur.
[self.selectedViewController popViewControllerAnimated:YES];
self.selectedIndex = 0;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
The interface auto rotates normally until you tap the button on tab #3.
Your help will be geatly appreciated!
iPhone SDK 3.2 solves this issue.
With previous SDK use [self.selectedViewController popViewControllerAnimated:NO].