Simple Title not showing up in UINavigationController - ios

I have looked at all of the similar/related questions, but none either a) are exactly my problem or 2) the solutions just don't work.
In my appDelegate.m I have in didFinishLaunchingWithOptions
JCGRootNavigationController *rnc = [[JCGRootNavigationController alloc] init];
self.window.rootViewController = rnc;`
JCGRootNavigationController is a subclass of UINavigationController
#interface JCGRootNavigationController : UINavigationController`
In JCGRootNavigationController.m:
#implementation JCGRootNavigationController
-(instancetype) init {
self = [super init];
self.view.backgroundColor = [UIColor lightGrayColor];
self.navigationItem.title = #"MY TITLE";
return self;
}
And the title just won't display. I see the Navigation Bar, but no title. Looks like lots of people over the years have had this same problem. Maybe a simple answer will help clear up all of the confusing. This is so incredibly frustrating.

When working with a Storyboard, setting the title of the UIViewController or UITableViewController does not seem to add that title to the Navigation Controller, as other answers suggest.
Instead, find the UINavigationItem that is likely alongside your View Controller object in the Storyboard hierarchy. Set this Navigation Item's title to apply that title to the Navigation Controller.

UINavigationController automatically shows the title of the UIViewController subclass it is displaying. It does so by looking at the navigationItem.title property of that UIViewController or UIViewController subclass. Basically UINavigationController doesn't have a title.

Thanks believesInSanta, While I cannot find this explicitly stated in apple documentation anywhere, I have to go with this being the answer. UINavigationController doesn't have a title.
To get the title to work, back in appDelegate.h I added:
JCGTableViewController *tvc = [[JCGTableViewController alloc] init];
JCGRootNavigationController *rnc = [[JCGRootNavigationController alloc] initWithRootViewController:tvc];
self.window.rootViewController = rnc;
Where JCGTableViewController is another subclass I added. As you can probably tell, it is a subclass of UITableViewController.
In JCGTableViewController I overrode init to:
-(instancetype) init {
self = [super init];
if(self) {
self.title = #"TVC";
self.view.backgroundColor = [UIColor lightGrayColor];
}
return self;
}
While I used a tableViewController, I imagine you can add any view to the NavigationController and set the properties that way. I will play around with that today.
Thanks all!

Related

Load Another ViewController In A Button From Main ViewController

before asking this question, I did some research of course. One of the links or topics I have found on google and on stackoverflow is this:
How do I load another view controller from the current view controller's implementation file?
In my case, I have two viewcontrollers in my project, namely:
ViewController (ViewController.h and ViewController.m) - my main vc
LeftViewController (LeftViewController.h and LeftViewController.m) - my second vc. It will be loaded upon clicking a button from my main vc.
Please take note that it is my 3rd day in my job, self training, and I find objective-c quite hard compared to others, like C# ... Oh I miss C#.
Going on, I tried following the steps from the links that I've got as well as the link I gave at the top.
Here are my codes:
a. Inside my ViewDidLoad in my ViewController.m
// add navigation button left
UIButton *leftNavigationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[leftNavigationButton setFrame:CGRectMake(10, 65, 30, 20)];
[leftNavigationButton setTitle:#"<" forState:UIControlStateNormal];
[leftNavigationButton addTarget:self action:#selector(navigateLeft) forControlEvents:UIControlEventTouchUpInside];
[leftNavigationButton.layer setBorderWidth:1.0f];
[leftNavigationButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[leftNavigationButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.view addSubview:leftNavigationButton];
b. the method inside my ViewController.m (of course outside the ViewDidLoad:
- (void)navigateLeft
{
// LeftViewController *leftViewController = [[LeftViewController alloc] init];
// [leftViewController viewDidLoad];
_leftViewController = [[LeftViewController alloc] initWithNibName:#"UI For LeftButton" bundle:nil];
[self.ViewController: self.leftViewController animated:YES completion:nil]; //Error in this line, spefically in self.ViewController<---
}
c. ViewController.h
//
// ViewController.h
// Navigation_Task
//
// Created by Glenn on 9/4/15.
// Copyright (c) 2015 Ayi. All rights reserved.
//
#import <UIKit/UIKit.h>
// for LeftViewController
#class LeftViewController;
#interface ViewController : UIViewController
#property (nonatomic, retain) NSMutableString *resultText;
// for LeftViewController
#property (strong, nonatomic)LeftViewController *leftViewController;
#end
and lastly, (this is not included in the link above), my code for loading the LeftViewController.
a. viewDidLoad in LeftViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
LeftViewController *leftViewController = [[LeftViewController alloc] initWithNibName:nil bundle:nil];
// set background and title of the view
[leftViewController.view setBackgroundColor:RGB(19,181,234)];
leftViewController.title = #"Left View Controller";
[self.navigationController pushViewController:leftViewController animated:YES];
}
Sorry if this question is too long, I can't shoot my questions to the developers here in the office, because I'm shy and they don't wanna be bothered :(
You are using right Controller for navigation i.e. UINavigationController, but to make it navigate I will recommend you to use storyboards. Take your UINavigationController as a rootviewcontroller and further take any UIViewController as its rootviewcontroller and then on 2nd UIViewController to navigate attach a Segue to it and provide it a identifier let say "PushToSecond" and in your buttons Method do the following:
(void)navigateLeft
{
[self performSegueWithIdentifier:#"PushToSecond" sender:self];
}
hope this would help
I suggest you take a look into Apple's documentation on UINavigation.
documentation
You can either add as many viewController objects into the navigation stack as needed or present it incase you need to show it temporarily.
I would like to answer my own question.
I have two buttons in my main ViewController.
1 button for LeftViewController
1 button for RightViewController.
Meaning I need to display each new viewcontroller or screen when I click 1 of those buttons.
Here are my codes:
Just inside the event handler (is my term correct? I'm from windows) of the two buttons, put the following:
Code for 'Left Screen/View Controller'
- (void)navigateLeft
{
// LeftViewController *leftViewController = [[LeftViewController alloc] init];
// [leftViewController viewDidLoad];
// _leftViewController = [[LeftViewController alloc] initWithNibName:#"UI For LeftButton" bundle:nil];
LeftViewController *leftViewController = [[LeftViewController alloc] initWithNibName:nil bundle:nil];
// set background and title of the view
[leftViewController.view setBackgroundColor:RGB(19,181,234)];
leftViewController.title = #"Left View Controller";
[self.navigationController pushViewController:leftViewController animated:YES];}
Similarly, I need to put the same type of codes (just renaming the leftViewController into RightViewController)
- (void)navigateRight
{
RightViewController *rightViewController = [[RightViewController alloc] initWithNibName:nil bundle:nil];
// set background and title of the view
[rightViewController.view setBackgroundColor:RGB(19,181,234)];
rightViewController.title = #"Right View Controller";
[self.navigationController pushViewController:rightViewController animated:YES];
}
Now I can push any of the two button and will display a new ViewController (or screen).

Nothing happens when I click UISearchBar in NavigationBar

I added a UISearchBar to my initial UIViewController and everything works fine. However when I go to add the same UISearchBar, the same way to another ViewController that's pushed ontop of the first one, nothing seems to happen when I click into the searchBar. No delegate method is fired, no keyboard comes up, nothing.
This is how I'm adding it to the navigationBar:
- (void)viewDidLoad {
[super viewDidLoad];
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchResults = [NSMutableArray array];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.placeholder = NSLocalizedString(#"Search, eh?", nil);
// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;
self.definesPresentationContext = YES;
}
Like I said it works in the first UIViewController, am I missing something in the other 2-3 viewControllers I've tried adding this too? I don't see why the searchBar shows up in the navBar but nothing is happening upon clicking into it. I've also set the delegate like so:
#interface ViewController () <UISearchResultsUpdating>
Try setting definesPresentationContext to NO on the first view controller as soon as you present a new one on.
I believe that the presentation walks up the view controller hierarchy looking for the first one that defines a context and not down.

Laying out views in a TabBarViewController

I'm trying to manually (programmatically) lay out views in a UITabBarViewController. I instantiate my UITabBarViewController like this:
MYAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
MYViewController1 *myViewController1 = [[MYViewController1 alloc] init];
myViewController1.title = #"My VC 1";
[tabBarController addChildViewController:myViewController1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = tabBarController;
return YES;
}
MYViewController1.m
- (void)viewDidLoad
{
[super viewDidLoad];
_myView = [[MYView alloc] initWithFrame:self.view.frame];
[self.view addSubview:_myView];
}
MYView.m
- (void)layoutSubviews
{
CGRect descriptionRect, buttonRect;
CGRectDivide(self.frame, &buttonRect, &descriptionRect, 50.f, CGRectMaxYEdge);
_descriptionTextView.frame = descriptionRect;
[self addSubview:_descriptionTextView];
_myButton.frame = buttonRect;
[self addSubview:_myButton];
}
The problem I'm having is that when I get to layoutSubviews, the superview's frame is the full size of the window, so the button is hidden by the tab bar. What am I doing wrong?
Without seeing more code, it appears that you may have some confusion about the use of a UITabBarController.
In the code you have posted above you are initializing a UITabBarController, then a UIViewController, and then you are calling 'addChildViewController:' on the tabBarController. (however, addChildViewController: is an instance method on UIViewController).
Is this in attempt to add the ViewController as a tab of the TabBarController? If so, then try the following code in place of addChildViewController: to see if it gives you the functionality that you are looking for:
tabBarController.viewControllers = #[myViewController1]; // Passing an array of viewControllers will set them as a tabs on the TabBar in the order they are added to the array.
If that doesn't help, then please comment on this answer with more details regarding the desired functionality of your code, and I'll update my answer to assist you as much as I can.
EDIT: Looks like I may have misunderstood your problem. Can you try the following code in play of your subview's initialization:
_myView = [[MYView alloc] initWithFrame:self.view.bounds]; // Try bounds instead of frame.

iOS TableView not displaying cells

I have seen several related questions, but mine is different in that I want to do it purely in coding, so without storyboards or interface builder.
Basically I get lost in how to properly display the cells in a TableView. I have created a very simple SplitView test application. In it I try to show the same TableView twice: once in the TableViewController and once in the DetailViewController (for a user this would make little sense, but I try to grasp how to get this done).
In the TableViewController, the 3 cells show correctly, but in the DetailView, the TableView shows without the cells. There must be a simple step to have the cells drawn, but all the example cases I find assume you use storyboards or IB
In the SplitView I have:
- (id) init{
self = [super init];
JBDetailViewController *detailVC = [JBDetailViewController controller];
UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detailVC];
JBTableViewController *rootVC2 = [[JBTableViewController alloc] init];
rootVC2.title = #"A TableVC title";
UINavigationController *rootNav2 = [[UINavigationController alloc] initWithRootViewController:rootVC2];
self.viewControllers = #[rootNav2, detailNav];
self.delegate = detailVC;
self.tabBarItem.title = #"a Tab title";
return self;
}
In the DetailViewer I have:
- (void) loadView {
[super loadView];
self.view.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.85f];
JBTableViewController *rootVC2 = [[JBTableViewController alloc] init];
[rootVC2 loadView];
rootVC2.title = #"Test van Justus";
rootVC2.view.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.85f];
[self.view addSubview:rootVC2.view];
}
Both use the same JBTableViewController that is very simple and should display 3 cells, which it does in the VC, but not in the DetailView.
What do I do wrong?

How do I change Nav Bar Title in a UITableView Detail View when an event occurs?

I have a UITableView that uses a UIWebView for its detail view. This detail view content has links. When a link is clicked, I want the nav bar title to change. How can I do this? I tried implementing the following UIWebView methods from my MainViewController and also in my AppDelegate.m file but neither fires when a link is clicked:
- (void)webViewDidStartLoad:(UIWebView *)webView{
UIViewController *detailsViewController = [[UIViewController alloc] init];
detailsViewController.title = #"New Title Here";
}
- (void)webView:shouldStartLoadWithRequest:navigationType{
UIViewController *detailsViewController = [[UIViewController alloc] init];
detailsViewController.title = #"New Title";
}
Thanks!
Just use
self.title = newTitle;
If you are pushing a new view controller (not clear from your question) put this in viewDidLoad:.

Resources