Using a UITabBar in a View-based app not working - ios

I have added a UITabBar to a View-based application, hooked up my View Controller as follows:
#interface SomeViewController : UIViewController<UITabBarDelegate> {
...
UITabBar *tabBar;
...
#property (nonatomic, retain) IBOutlet UITabBar *tabBar;
Inside my implementation file I have done this:
#synthesize tabBar;
- (void)tabBar: (UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
}
I went into IB and hooked tabBar up to File's Owner tabBar.
My problem: clicking on a tab bar item never fires off the didSelectItem method. Am I missing or mis-doing a step?

Did you set the delegate of the tabbar to self?

Related

Tab bar without TabBarController - add View Controller for Tab bar item in storyboard

I have added a Tab bar (not a TabViewController) to a View Controller and then added some Tab bar items to that Tab bar.
Now I want to attach other View Controllers to those tab bar items in Storyboard.
When I do Ctrl + Drag to View Controller from tab bar item I do not get any options.
Please suggest a way to do this.
I had the same problem, but i couldn't find a way to assign to a viewController its own viewControllers as in the TabViewController case.
I solved it using containers. One contarner for each tabBarItem in your tabBar, which are hidden or showed depending of the selected tabBarItem in the tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item method.
1. Create your containers in your UIviewController in storyBoard:
Just like this Select your tabBar and Ctrl+Drag to delegate the class for listen the tabBarDelegate methods: look here
2. Declare the corrisponging IBOutlets, incliding your tabBAr:
#import <UIKit/UIKit.h>
#interface TabsMainViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITabBar *tabBar;
#property (strong, nonatomic) IBOutlet UIView *directoryContainer;
#property (strong, nonatomic) IBOutlet UIView *groupsContainer;
#end
3. Select the container to show in the tabBarDelegate method:
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
case 1:
_directoryContainer.hidden = NO;
_groupsContainer.hidden = YES;
break;
case 2:
_directoryContainer.hidden = YES;
_groupsContainer.hidden = NO;
break;
default:
break;
}
}
Hope that helps!

is it possible to perform a segue from tab bar item?

I used a UIview controller for my app home page and then added a tab bar at the bottom just like Facebook and then added 3 more tab bar item, it doesn't let me perform a segue when drag the tab bar item to a View Controller, is it possible progmatically or in storyboard?
I had the same problem, but i couldn't find a way to assign to a viewController its own viewControllers as in the TabViewController case.
I solved it using containers. One container for each tabBarItem in your tabBar, which are hidden or showed depending of the selected tabBarItem in the tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item method.
1. Create your containers in your UIviewController in storyBoard: Just like this Select your tabBar and Ctrl+Drag to delegate the class for listen the tabBarDelegate methods: look here
2. Declare the corrisponging IBOutlets, incliding your tabBAr:
#import <UIKit/UIKit.h>
#interface TabsMainViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITabBar *tabBar;
#property (strong, nonatomic) IBOutlet UIView *directoryContainer;
#property (strong, nonatomic) IBOutlet UIView *groupsContainer;
#end
3. Select the container to show in the tabBarDelegate method:
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
case 1:
_directoryContainer.hidden = NO;
_groupsContainer.hidden = YES;
break;
case 2:
_directoryContainer.hidden = YES;
_groupsContainer.hidden = NO;
break;
default:
break;
}
}
Hope that helps!
Simple: You need a UITabViewController, tab bar items can't be used the way you're asking for.
Ctrl+drag from your tabview controller to a view you'd like to include (Third in this case)
You then select the view controllers option to add the relationship segue.

Pushing view controller is not working properly in ios

I am passing textfield data between two view controllers. i have two view controllers Firstviewcontroller, Secondviewcontroller. i have embed them in navigation but when i click the button on first view controller, second view controller is not showing only black screen is showing.below is my code.
First view controller.h
#import <UIKit/UIKit.h>
#import "secondViewController.h"
#interface FirstViewController : UIViewController<SecondViewControllerDelegate>
#property(nonatomic) secondViewController *secondview;
- (IBAction)btnclick:(id)sender;
#property (strong, nonatomic) IBOutlet UITextField *textfield1;
#end
First view controller.m
- (IBAction)btnclick:(id)sender {
secondViewController *SecondViewController= [[secondViewController alloc]init];
SecondViewController.data=self.textfield1.text;
[self.navigationController pushViewController:SecondViewController animated:YES];
}
- (void)dataFromController:(NSString *)data
{
[self.textfield1 setText:[NSString stringWithFormat:#"%#",data]];
}
Seconviewcontroller.h
#protocol SecondViewControllerDelegate <NSObject>
#required
- (void)dataFromController:(NSString *)data;
#end
#interface secondViewController : UIViewController
#property (nonatomic, retain) NSString *data;
#property(nonatomic,weak)id<SecondViewControllerDelegate> _delegate;
#property (strong, nonatomic) IBOutlet UITextField *textfield2;
#end
Seconviewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
self.textfield2.text=[NSString stringWithFormat:#"%#",_data];
// Do any additional setup after loading the view.
}
whats the reason ??
You have to either use storyboard and let it intanciate the view controller for you:
[self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"]
Or you can use xib files and tell the view controller which one to load:
[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil]
second view controller is not showing only black screen is showing
Typically, this means that the view controller has no view. Your code does not explain where secondViewController is expected to get its view from. Is there a secondViewController.xib? If there is, is it correctly configured with a view outlet? If not, what does this view controller do in order to get its view?
Note that you have done a very odd thing with capitalization: you have given your class a small letter (secondViewController), while your instance has a capital latter (SecondViewController). This is backwards and a big mistake, and you should correct it immediately. Perhaps the problem is just a capitalization problem; if you call your class secondViewController and your xib file SecondViewController, that is not a match and they won't find each other.
I see "IBOutlet" in your code, it means you created the user interface of secondviewcontroller on storyboard. I guess [[secondViewController alloc]init] does not get UI element from storyboard then the black screen is shown. You have to create instance of secondViewController by loading from storyboard file.
Basically:
you can get storyboard via storyboard file name
on storyboard, Secondviewcontroller has an storyboard id which is set in Identity Inspector and you can use it to get instance of Secondviewcontroller
You can refer this link for more detail
loading iOS viewcontroller from storyboard
Hope this can help!

Implement custom UITabBar to multiple UIViewController's

I am trying to include a tab bar, with 4-5 tab bar items on it, to multiple view controllers of my app and will work as menu to jump between views (Map, About, Favourites etc.).
I have created on Storyboard a UITabBar item and set its Bar item's tags. Because the same tab bar will be used on several other view controllers (Main, View2, View3 etc.) I've decided to create a class that extends UITabBar. This will help me to customise the bar later. The UITabBar object in Storyboard is now an object of this class (BottomTabBar).
My question is, how I can detect when a bar item has been tapped?
In addition, because I am not familiar with TabBar, if you have any general guides or tips that will help me during development please share them with me.
BottomTabBar.h
#import <UIKit/UIKit.h>
#interface BottomTabBar : UITabBar <UITabBarDelegate>
#end
BottomTabBar.m
#import "BottomTabBar.h"
#implementation BottomTabBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.delegate = self;
}
return self;
}
- (void) tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(#"Tabbed!");
}
#end
MainViewController.h
#import <UIKit/UIKit.h>
#import "BottomTabBar.h"
#interface MainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
AppDelegate *appDelegate;
NSArray *searchResults;
}
#property (strong, nonatomic) IBOutlet UIScrollView *slideshow;
#property (strong, nonatomic) IBOutlet UIPageControl *scroll;
#property (strong, nonatomic) IBOutlet BottomTabBar *bottomBar;
#end
This guide is really good:
http://www.rumex.it/2010/07/how-to-customise-the-tab-bar-uitabbar-in-an-iphone-application-part-1-of-2/
Notice there is also a second part there.
See this tutorials and source codes for custom tab bar.
Custom tabbar
I hope it helps you.

How to add a NavigationBarItem and UIBarButtonItem programmatically?

I presented a View using a call to presentModalViewController.
My new View didn't have a navigation bar, so I added one from the Library but now when I created a UIBarButtonItem in viewDidLoad of my controller, and set it to rightBarButtonItem of the navigationBarItem.
But when I run my app, the Navigation bar is there but there is no button.
Did I miss a step or something ?
Just adding a navigation bar manually will not make the self.navigationController property actual and it's value is nil, which, I believe, you're using for adding a UIBarButtonItem instance.
What I'd recommend is to declare a UINavigationBar IBOutlet in your controller, connect it in NIB editor with navigation bar you've added and after that in viewDidLoad use something like:
#interface ...
#property (nonatomic, retain) IBOutlet UINavigationBar *theBar;
#end
#implementation ...
#synthesize theBar;
-(void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *yourButton = ...;
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"MyTitle"];
item.rightBarButtonItem = yourButton;
[theBar pushNavigationItem: item animated:NO];
[item release];
...
}
#end
I thought
navigationBarItem property of UIViewController is work only if your ViewController is present with UINavigationController
if you add a navigation bar with interface builder
or add it with code ,then you need to use
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated
to add UINavigationItem yourself
you can create a UINavigationController
use - (id)initWithRootViewController:(UIViewController *)yourViewController
and use presentModalViewController to show UINavigationController that you create
then you can use navigationBarItem property of UIViewController to set rightBarButtonItem in viewDidLoad
Actually it is easier than this:
Drag a UINavigationBar into your app. Note that you will also get a UINavigationItem when you do this... make sure you keep it.
Add the following property to your app:
#property (weak, nonatomic) IBOutlet UINavigationItem *navigationItem;
Xcode will likely indicate an error (you are overriding a built in property of UIViewController), so you have to manually synthesize the getter and setter. Add the following line below the #implementation statement for the class:
#synthesize navigationItem;
In Interface Builder, drag a connection from the new navigationItem property to your custom navigation item.
You can now access your navigation item using self.navigationItem, just like you would normally.

Resources