Adding a UITabBarController and UITabs programmatically - iphone

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.

Related

How to display UISplitViewcontroller on button click

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

pushViewController doesnt work even though i have a navigationController

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 ?

make UITabBar without adding in appDelegate file

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];
}

how to push UIImagePickerController into UINavigationController?

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!

Implement Back action into UITabBarController

I have a controllerView (MenuControllerView) with a button inside, when I click on the button a new ViewController will appear with a TabBarController created programmatically like this:
UIView* topView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
tabBarController = [[UITabBarController alloc] init];
viewController1 = [[ViewController1 alloc] init];
viewController2 = [[ViewController2 alloc] init];
viewController3 = [[ViewController3 alloc] init];
viewController4 = [[ViewController4 alloc] init];
tabBarController,viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 ,viewController4, nil];
[[self tabBarController] setSelectedIndex:1];
[topView addSubView:[tabBarController view]];
Instead of displaying ViewController1 for the first button Item, I want to put an action Back in it to return to my MenuViewController, but I don't know how how to do it.
Thanks
Have you considered presenting the UITabBarController as a modal view controller and implementing UITabBarControllerDelegate? e.g. this seems to work for me (I make the third tab return to MenuViewController here):
#interface MenuViewController : UIViewController <UITabBarControllerDelegate>
...
- (IBAction) onButtonPressed:(id)sender
{
UITabBarController* tabBarController = [[UITabBarController alloc] init];
viewController1 = [[ViewController1 alloc] init];
viewController2 = [[ViewController2 alloc] init];
viewController3 = [[ViewController3 alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 , nil];
[[self tabBarController] setSelectedIndex:1];
tabBarController.delegate = self;
[self presentModalViewController:tabBarController animated:NO];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
{
if (viewController == viewController3)
{
[self dismissModalViewControllerAnimated:NO];
return NO;
}
return YES;
}
I doubt that this approach is a good one. You'll gonna break typical iPhone behaviour which will confuse users. The TabBarController is designed (functionally and technically) to change between views while a NavigationController is for pushing and popping views (go forth and back). Of course you can combine those (which is not always easy), but you shouldn't use TabBar as NavigationBar.
if I understand right, you can just remove your tabbar's view from superview. smth like
[[tabBarController view] removeFromSuperview];
if you just want to handle selection of tabbar item, you can use tabBar:didSelectItem: method of th UITabBarDelegate protocol.
Is this what you're trying to do?
This automatically created with a UINavigationController upon pushing to a child view controller.
[self.navigationController pushViewController:yourChildViewController animated:YES];