How to add a NavigationBarItem and UIBarButtonItem programmatically? - ios

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.

Related

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.

UINavigationController nested in UITabBar, select a particular index don't work

i have a UInavigationController nested in uitabbar and i need to push a particular view ( the second element of tabbar, index:1). (I use storyboard)
I've already tried:
UITabBarItem *tabBarItem1 = [tabBarGlobal.items objectAtIndex:1];
[tabBarGlobl setSelectedItem:tabBarItem1];
And for setup appearance i've used:
[self tabBar:self.tabBar didSelectItem:tabBarItem1];
The appearance is good but in the tabbar the element selected is the first and not the second.
tabBarGlobal is connected with the view in Storyboard:
#property (strong, nonatomic) IBOutlet UITabBar *tabBarGlobale;
Any Idea?
Thank's.
If you're not using your UITabBar with an UITabBarController, you should read this post first.
And then, you have to set this UITabBarController property:
#property(nonatomic) NSUInteger selectedIndex
See here

Can't set UINavigationItem title programmatically

I set up my app using a storyboard and have my main view controller embedded in a UINavigationControler. To change the title that appears in the navigation bar, in the viewDidLoad method of my main view controller, I have self.navigationItem.title = #"My Title"; But the title never gets set.
My guess is that I need to set up a Reference Outlet in my storyboard, but I'm not sure what needs to be connected to what. Any ideas?
in story board, it doesn't get automatically connected , make a UINavigationItem using the following code in ur .h file
#property(weak, nonatomic) IBOutlet UINavigationItem *navBar;
in .m file synthesize the property and set the title like this
#synthesize navBar;
-(void)viewWillAppear:(BOOL)animated {
[self.navBar setTitle:#"Sign In"];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
Also, don't forget to connect the UINavigationItem "navBar" in the storyBoard with ur class so that storyboard knows whose title to change. In case u don't have a UINavigationItem in ur storyboard,add it outside the UIView and then connect it properly
Just setting the title property of the view controller you are in should do the trick.
[self setTitle:#"Best Page Ever"];
After you have allocated your rootview..use:-
YourRootView *rootView=[YourRootView alloc]init];
rootView.title=#"yourTitle";
and then initialize it in uinavigationcontroller.(I don't have idea if you have your view on storyboard , then set its title just after allocating and then pass it as parameter to navigationcontroller).

Using a UITabBar in a View-based app not working

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?

Automatically highlight UITabBar Button

I'm using this method http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited to create a custom UITabBar and loading a particular view when a certain TabBarItem is clicked.
The problem is that initially the first view is loaded, but the first tab bar item is not highlighted. Is there a way to force the highlight? I'm not using the tabbarcontroller so I can't use its methods.
If you make an instance variable: UITabBar *tabBar;
a property:
#property (nonatomic, assign) IBOutlet UITabBar *tabBar;
and connect this property to the UITabBar in Interface Builder, you can use:
for(UITabBarItem *tab in tabBar.items) {
if ([tab.title isEqualToString: #"My Tab Title"]) {
tabBar.selectedItem = tab;
}
}
This works if all tabs have a unique title, which is normally the case.

Resources