Can't set UINavigationItem title programmatically - ios

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).

Related

How to set NavigationController navigationItem title from ViewController loaded by Xib

I've a UIViewController which loads it's view form a Xib file. In my AppDelegate.m I would like to initiate my rootViewController with that myViewController and I would like that the title of the navigtionItem is set by taking the NavigationBar view which is part of the Xib file of the myViewController:
MyViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
According to the documentation
Each time the top-level view controller changes, the navigation controller updates the navigation bar accordingly.
In my setup this seems not to happen or I haven't indicated that my NavigationBar view is actually the navigationBar to consider.
How can I tell the NavigationController to update it's navigationItem content (title, left and right bar buttons) according the NavigationBar view which is part of the Xib file from myViewController?
Update
My Xib layout looks like this:
I know I can set those items in code by:
self.navigationItem.titleView = ...;
self.navigationItem.leftBarButtonItem = ...;
But I want them do design in the Xib file of the ViewController and they should be used in the NavigationController.
In ViewWillAppear method in your ViewController set-
self.navigationItem.title = #"Title name";
Also in the same way you can set self.navigationItem.leftBarButton and RightBarButton items
Adding a UINavigationBar view to the main UIView doesn't do what you think it does. It merely adds a navigation bar view there in the view, it does not tie into the UINavigationController.
If you use a storyboard, you can add the UINavigationItem as a child of the UIViewController. But since you are not using a storyboard, the easiest way to do it is to update the navigationItem in viewDidLoad.
Sidenote: Apple does not recommend using viewDidLoad to setup the navigation item. If you really want to do it the proper way, you can for example do it in the navigationItem getter.
- (UINavigationItem*)navigationItem
{
// get super navigation item
UINavigationItem *navItem = [super navigationItem];
// do stuff
return navItem;
}

Display UIview controller as a subview using storyboard IOS7

in my application I have a UIView object in second view controller. Now I want to display the UIView as a popup or sub view when I click the button from first view controller. Please tell me how to do this? I have seen many solution for the nib files but I didn't find any for storyboard.
I have connected the IBOulet of view in my second view controller.
#property (strong, nonatomic) IBOutlet UIView *popview;
And i have imported the second view controller in my firstview please tell me how to make this one i have been stuck here for long time please help me out on this one.
Thanks.
In your FirstViewController which has UIView *popview:
- (void)viewDidLoad
{
[super viewDidLoad];
// you don't want to show PopView
self.popview.alpha = 0;
}
- (void) showPopView
{
//you want to show PopView
self.popview.alpha = 1;
}
Try Adding this in your code
-(void)viewDidLoad
{
popView.hidden=true;
}
and when ever user clicks an action change the hidden property to false just like
-(IBAction)AnyActionPerferformed:(id)sender
{
popView.hidden=false;
}
You can Learn about this from
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html
You don't need an extra UIViewController
In your storyboard, drag a new UIView to your view controller. (It will be your popup view)
Link to your view controller. (#property (strong, nonatomic) IBOutlet UIView *popview;)
When you want to hide popview set it's alpha to 0.

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.

iOS: popping up a subview in a tabview

Newbie question for iOS -- I'm really confused with how navigation view works in a tabview.
Right now I have a tabview that has 2 views. In the second tab I have a button. When the button is clicked I'd like a new window to show up with some information, and the new window needs a Back button on top that goes back to the second tab.
I followed some tutorials and put a NavigationController in secondTab.xib, added the line
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
to secondTab.h, and
NewWindowController *newWindow = [[NewWindowController alloc] initWithNibName:#"NewWindowController" bundle: nil];
[self.navController pushViewController:newWindow animated:YES];
NSLog(#"clicked");
to my button implementation for -(IBAction) click: (id)sender
When I clicked the button in my second tab, the log shows "clicked" but my view is not changing.
Is there some setting I need to change for File's Owner/Navigation Controller outlets/referencing outlets etc...?
Thanks!
You don't want a property for the UINavigationController, you want to push onto the current navigation controller like so:
NewWindowController *newWindow = [[NewWindowController alloc] initWithNibName:#"NewWindowController" bundle: nil];
[self.navigationController pushViewController:newWindow animated:YES];
NSLog(#"clicked");
When a UIViewController is associated with a UINavigationController (i.e. it's part of a navigation controller hierarchy) then its navigationController property will be set, so you can access it like I've shown.

Programmatically add UINavigationController in UIViewController

I currently have a view set up as the following:
#interface BlogViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *mainTableView;
}
#property (nonatomic, retain) UITableView *mainTableView;
As you can see, it has a UITableView inside of it that I load all of my data to. However, when I call the following function:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SingleBlogViewController *viewController = [[SingleBlogViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
//[self presentModalViewController:viewController animated:YES];
[viewController release];
}
nothing happens. For some reason my UITableView inside of my UIViewController isn't pushing the view. Is this because it isn't a UITableViewController? I tried changing it to that, and it still didn't work.
Am I missing something here?
I found the first parts of this blog post useful for showing how to create and use a UINavigationController programmatically without Interface Builder.
Some of the things I wish the docs and tutorials would have stressed to me:
When you create the UINavigationController you get a UIView and UINavigationBar for free (i.e. you don't need to separately add them and figure out how to hook them together).
You add the myUINavigationController.view property as the "main" view and then push/pop UIViewControllers onto the UINavigationController and they will automatically show up as visible in the myUINavigationController.view UIView.
When you push a UIViewController onto a UINavigationController, the UIViewController.navigationController is filled in. If you haven't pushed the view onto the navigation controller, I'm guessing that property is empty.
The time/place to programmatically add buttons to the UINavigationBar is not when and where you construct the UINavigationController, but rather by the individual UIViewControllers when they are loaded as you push onto the UINavigationController.
I only had to add a "Done" button to the UINavigationController's UINavigationBar in the viewDidLoad method of the first UIViewController pushed onto the UINavigationController. Any UIViewController I pushed on top of that first view automatically had a "back" button in the to navigate to the covered up UIViewController.
If you set the title property of your UIViewController before you put it on the UINavigationController's stack. The UINavigationBars title will be the title of your view. As well any "back" buttons will automatically name the view you are returning to instead of generically saying "back".
Please visit "How to add UINavigationController Programmatically"
Please visit above link & get all information regarding UINavigationController.
UINavigationController is a subclass of UIViewController, but unlike UIViewController it’s not usually meant for you to subclass. This is because navigation controller itself is rarely customized beyond the visuals of the nav bar. An instance of UINavigationController can be created either in code or in an XIB file with relative ease.

Resources