UITabBar Title Not Showing - iphone

I've implemented a UITabBar programmatically exactly the way Apple recommends, and I can't get the title to show up. Here is how I'm implementing it:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = [[UITabBarController alloc] init];
MainViewController *mainViewController = [[MainViewController alloc] init];
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController: mainViewController];
[[mainNavigationController navigationBar] setBarStyle: UIBarStyleBlack];
[tabBarController setViewControllers: [NSArray arrayWithObjects: mainNavigationController, nil]];
[self.window setRootViewController: tabBarController];
[self.window makeKeyAndVisible];
[mainViewController release];
[mainNavigationController release];
[tabBarController release];
return YES;
}
Here is the init method of mainViewController where I add the title:
#implementation MainViewController
- (id) init
{
self = [super initWithNibName: nil bundle: nil];
if (self)
{
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Main" image: nil tag: 1];
[self setTabBarItem: tabBarItem];
[tabBarItem release];
}
return self;
}
I noticed there are a bunch of other questions on this, and some have hacky type solutions, but I'm wondering if Apple is recommending this way, why isn't it working??

This can be done in a variety of ways. The simplest one is to try setting
self.title = #"Main";
This should be reflected in both your navigationBar title and tabBarItem title.
Or , you could just connect them to the interface builder and type it out yourself and connect the outlets properly.

Related

Setting the bottom bar as a tab bar programmatically in Objective-c

In IB, I can set the bottom bar as a tab bar for a view as below screen shot, but how can I set it by code in the implement(.m) file?
Thanks
Create an NSArray of the UIViewControllers that you want to use. Then instantiate a UITabBarController and set the viewControllers property to this array. Then add the view of the tabBarController to the window. All this should be done in the AppDelegate.m file. For example:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *vc1 = [[UIViewController alloc] init];
UIViewController *vc2 = [[UIViewController alloc] init];
CustomViewController *vc3 = [[CustomViewController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, vc3, nil];
[vc1 release]; [vc2 release]; [vc3 release];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:viewControllers];
[window addSubview:[tabBarController view]];
[window makeKeyAndVisible];
return YES;
}

NavigationBar not appearing with TTThumbsViewController in UITabBarController

I'm trying to put a TTThumbsViewController inside a UITabBarController, but when I do, the TTThumbsViewController's NavigationBar doesn't show. There is just blank space where the NavigationBar should be. I've loaded just the TTThumbsViewController by itself, and the NavigationBar loads just fine. I'm sure I've just missed a setting, but I can't figure out what it is.
Here is what I'm doing to create the UITabBarController and the TTThumbsViewController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.tabBarController = [[UITabBarController alloc] init];
ThumbsViewController *thumbsViewController = [[ThumbsViewController alloc] init];
UITabBarItem *thumbsTabBarItem = [[UITabBarItem alloc] initWithTitle:#"Thumbs" image:[UIImage imageNamed:#"icon.png"] tag:Thumbs];
thumbsViewController.tabBarItem = thumbsTabBarItem;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:thumbsViewController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
If you're loading the TTThumbsViewController from a UITabController, you need to create the UINavigationController yourself.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.tabBarController = [[UITabBarController alloc] init];
ThumbsViewController *thumbsViewController = [[ThumbsViewController alloc] init];
UITabBarItem *thumbsTabBarItem = [[UITabBarItem alloc] initWithTitle:#"Thumbs" image:[UIImage imageNamed:#"icon.png"] tag:Thumbs];
thumbsViewController.tabBarItem = thumbsTabBarItem;
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:ThumbsViewController] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

ModalView doesnt show up with tabbarcontroller

I am building an app that has a login screen that leads to a tabbar. I followed this example on how to push a modal view once you launch the app (as the "sign in" page) and then dismiss it.
Example --> Show / Hide tab bar
From some reason, it's not really working - when I launch the app, I see the tabbar view with the two view controllers. no sign in page.
here is my code:
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:#"FirstTab" bundle:NSBundle.mainBundle];
UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:#"SecondTab" bundle:NSBundle.mainBundle];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, secondNavController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
My first tab (which is where I understand all this modal view business needs to happen)
.h
#interface FirstTab : UIViewController
#end
.m
//UPDATED CODE PER COMMENT BELOW
- (void)viewDidLoad
{
[super viewDidLoad];
SignIn *loginview = [[SignIn alloc] initWithNibName:#"SignIn" bundle:nil];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController: loginview];
self.hidesBottomBarWhenPushed = YES;
[self presentModalViewController:controller animated:YES];
}
And of course, I dismiss the modal view in the SignIn view controller, though I never get there as I mentioned.
What am I doing wrong here? Thanks for the help!!
You could use :
[[self tabBarController] presentModalViewController:controller animated:YES];
since first viewController1 is your first tab, and self.navigationController might be nil.
In your custom view controller subclass called SignIn implement initWithNibName:bundle: instead if init.
-(id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// init here
}
}
Now when init/alloc it call either :
SignIn *loginview = [[SignIn alloc] initWithNibName:#"SignIn" bundle:nil];
if your interface is in a NIB file, or :
SignIn *loginview = [[SignIn alloc] initWithNibName:nil bundle:nil];
if there no NIB.
Also why putting it as a root view controller of any navigation controller ? unless you need to go deeper in some model data presentation, just present it directly :
// why ?
//UINavigationController *controller = [[UINavigationController alloc]
// initWithRootViewController: loginview];
//self.hidesBottomBarWhenPushed = YES;
//[self presentModalViewController:controller animated:YES];
[self presentModalViewController:loginView animated:YES];
You have to include this in your code,
yourModelController =[[Yourmodelcontroller alloc]init];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController: yourModelController];
self.hidesBottomBarWhenPushed = YES;
[[self navigationController] presentModalViewController:controller animated:YES];
[controller release];
hope this will help you out.
I've encountered issues where modals don't like showing from viewDidLoad. Try adding your code to your viewWillAppear and it should show.

How to Use TabBarViewController with a NavigationController

I know I can do this
[self.navigationController pushViewController:self.someUITabBarController animated:YES];
And that means putting UITabBarController on a navigationgController somehow
What about if I want someUITabBarController to be the first controller (the one located on the lowest level) of navigationController?
I simply cannot change the rootViewController of the NavigationController into someUITabBarController
Erm not sure this is what you want. Below this code will be put under the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in you "appDelegate" class.
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = ...
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController1];
NSArray *controllers = [NSArray arrayWithObjects:navigationController, nil]; // can add more if you want
[tabController setViewControllers:controllers];
// this is for custom title and image in the tabBar item
navigationController.tabBarItem.title = #"abc";
[navigationController.tabBarItem setImage:[UIImage imageNamed:#"abc.png"]];
self.window.rootViewController = tabController; // or [self.window addSubview: tabController.view];
[self.window makeKeyAndVisible];
I am not sure if this works. But try this,
UINavigationController *navCont = [[UINavigationController alloc] init];
[navCont pushViewController:navCont animated:NO];

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!