Why won't this UIToolBar appear? - ios

Here is the init method of the root view controller of my navigation controller that I am trying to display the toolbar on:
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nil bundle:nil];
if(self){
//GUI implementation
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:nil];
self.toolbarItems = [NSArray arrayWithObjects:flexiableItem, item1, nil];
UIBarButtonItem* addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addEmployee)];
self.navigationItem.rightBarButtonItem = addButton;
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
return self;
}
and here is my delegate application:DidFinishLaunching method in the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
BOHomeViewController* hvc = [[BOHomeViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController* navbar = [[UINavigationController alloc] initWithRootViewController:hvc];
[self.window setRootViewController:navbar];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
The toolbar is not showing up at all. Could someone point out where I am doing something wrong? Much appreciated.

Move the initializing of your toolbarItems to the initializer you're actually calling ( initWithTableViewStyle: ) and self.navigationController.toolbarHidden = NO; to viewWillAppear as self.navigationController will be nil in your intializer.

Because you are not calling it!
You are calling
BOHomeViewController* hvc = [[BOHomeViewController alloc] initWithStyle:UITableViewStylePlain];
where you should be calling
BOHomeViewController* hvc = [[BOHomeViewController alloc] initWithNibName:<"Some Name"> bundle:<"Some Bundle">];

As far as I can see, initWithNibName is no called. Yoc call initWithStlye.
And even then - at that point in time your hvc is not part of a navigation hierarchy. Meaning self.navigationController should be nil during init. It gets value as soon as it is assigned as rootviewcontroller to the later newly created UINavigationController.

Related

How to set rootViewController for navigation view controller and tabbar controller in AppDelegate

i have this problem. When i set rootViewController for TabbarController, it show correctly. But i set another rootViewController for navigation bar, TabbarController will not able to display. Any idea?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[DCTabBarController alloc] init];
DCTabBarController *tabBar = (DCTabBarController *)self.window.rootViewController;
[tabBar setSelectedIndex:2];
Map_ViewController *vc = [[Map_ViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:vc];
[rootNav.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbarBackImage"] forBarMetrics:UIBarMetricsDefault];
rootNav.navigationBar.tintColor = [UIColor whiteColor];
[rootNav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
LeftViewController *leftVC = [[LeftViewController alloc] init];
RightViewController *rightVC = [[RightViewController alloc] init];
XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:rootNav];
slideMenu.leftViewController = leftVC;
slideMenu.rightViewController = rightVC;
self.window.rootViewController = slideMenu;
[self.window makeKeyAndVisible];
return YES;
}
After applied Adeel solution, here is the output.But items in tabbar will not display accordingly.
One important thing to mention here is that an application's window can have only one rootViewController (of course). Like I said in my comment as well, you probably want to do something like this.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
Map_ViewController *vc = [[Map_ViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:vc];
[rootNav.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbarBackImage"] forBarMetrics:UIBarMetricsDefault];
rootNav.navigationBar.tintColor = [UIColor whiteColor];
[rootNav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
DCTabBarController *tabBar = [[DCTabBarController alloc] init];
[tabBar setViewControllers:#[rootNav]];
self.window.rootViewController = tabBar;
LeftViewController *leftVC = [[LeftViewController alloc] init];
RightViewController *rightVC = [[RightViewController alloc] init];
XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:tabBar];
slideMenu.leftViewController = leftVC;
slideMenu.rightViewController = rightVC;
self.window.rootViewController = slideMenu;
[self.window makeKeyAndVisible];
return YES;
}

BarButtonItem on NavigationBar is forced to be transparent if returned with popViewController

I have a problem about UIBarButtonItem on NavigationBar.
Transit from FirstViewController to SeconderViewController by pusuViewController.
Return to FirstViewController with return button on left side of NavigationBar.
[Problem happen] The Color of [next] button on right side of NavigationBar is transparent.
(You can tap [next] button althought the color is transparent )
This problem happen on iPhone8(iOS11.2.1(15C153), not heppen on iPhone6(iOS10.3.3(14G60)).
My code is below,
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.backgroundColor = UIColor.blackColor;
UIViewController *vc = [[FirstViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];
return YES;
}
FirstViewContrtoller.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"First View";
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"next", nil)
style:UIBarButtonItemStylePlain
target:self
action:#selector(touchUpNextButton:)];
self.navigationItem.rightBarButtonItem = nextButton;
}
- (void)touchUpNextButton:(id)sender
{
UIViewController *vc = [[SecondViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = #"Second View";
}
I'd appreciate if you would provide me a good solution.
Thank you.
I think it is the bugs of iOS 11.2.1
You can temporary fix by following solution:
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"next", nil)
style:UIBarButtonItemStylePlain
target:self
action:#selector(touchUpNextButton:)];
[nextButton setTitleTextAttributes:#{NSForegroundColorAttributeName : [self.view tintColor], NSFontAttributeName:[UIFont systemFontOfSize:16.9f]} forState:UIControlStateNormal];
Hope that can help you.

Can't pop iOS viewController. Not sure, but I think it's something with the Navigation Controller

I'm having trouble trying to pop a view
App Delegate
#implementation MAAppDelegate
#synthesize navController;
#synthesize detailViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Init the navController for the Master Detail View of the grade cells
UINavigationController *navController = [[UINavigationController alloc] init];
detailViewController = [[UIViewController alloc] init]; //step6
navController = [[UINavigationController alloc] initWithRootViewController:[[MAController alloc] init]]; //step7
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = navController; //step8
[self.window makeKeyAndVisible];
// Set MAController as rootViewController
//self.window.rootViewController = [[MAController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// Use the insanely cool TSMessages to show network alerts
[TSMessage setDefaultViewController: self.window.rootViewController];
return YES;
}
First part of viewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController setNavigationBarHidden:YES];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStyleBordered target:self action:#selector(home:)];
self.navigationItem.leftBarButtonItem=newBackButton;
Later, when I change the viewController
NSLog(#"Opened progress report");
UIViewController *detailViewControl = [[UIViewController alloc] init];
// Set progress report as the view controller
[self.navigationController pushViewController:detailViewControl animated:YES];
UIImage *background = [UIImage imageNamed:#"bg"];
// Add static image bg
self.backgroundImageView = [[UIImageView alloc] initWithImage:background];
self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:self.backgroundImageView];
// Add blurred layer to image when tableView goes in front of it
self.blurredImageView = [[UIImageView alloc] init];
self.blurredImageView.contentMode = UIViewContentModeScaleAspectFill;
self.blurredImageView.alpha = 0;
[self.blurredImageView setImageToBlur:background blurRadius:10 completionBlock:nil];
[self.view addSubview:self.blurredImageView];
[self.navigationController setNavigationBarHidden:NO];
So I don't understand why that when I do this, a selector from the button (that I know fires, because I get Righthtere in my log):
-(void)home:(UIBarButtonItem *)sender {
NSLog(#"Righthtere");
// Set progress report as the view controller
[self.navigationController popToViewController:self animated:YES];
}
It doesn't go back to the initial view controller.
You seem to be confusing popToViewController and popViewControllerAnimated. popViewControllerAnimated removes the current view from the stack and brings the new stack top the active view controller. popToViewController pops the stack until the listed view controller is on top of the stack.
Since you are calling popToViewController with self, it will look and see that the requested view controller is already on top of the stack and do nothing. If you wish to go back one view controller then your call should be.
[self.navigationController popViewControllerAnimated:YES];
I use the below code to pop the previous viewcontroller in iOS 8.
[self presentModalViewController:viewcontroller animated:YES];

How to create a UINavigationBar programmatically

I've recently started learning Obj-C and according to my friend (who is experienced with Obj-C), it's better if I learnt how to create a view programmatically and not rely on StoryBoards.
So... I've been trying to create a NavigationBar with a left button on it. But, when I run it, I get the actual NavigationBar, but not the button. Here's my code:
The AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
firstController = [[MainViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:firstController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
and here's the MainViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
UIColor *navbarColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = navbarColor;
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:#"Settings" style:UIBarButtonItemStylePlain target:self action:NULL];
self.navigationController.navigationItem.leftBarButtonItem = button;
}
Any help would be appreciated. I'm fairly new to Objective-C but I understand the code concept, so you don't need to "baby" talk it when explaining ^.^
add this line before [self.window makeKeyAndVisible];
[self.window addSubview:navController.view];

TabBar application with NavigationBar

I'm building an App which is TabBar based. One of the tabs will display a TableView and I would like to have a NavigationBar at the top.
However I need to localize everything in this app to several languages so it needs to be done in code.
I have the tab bar set up in the AppDelegate;
#implementation AppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:#"HomeView" bundle:nil];
UIViewController *viewController2 = [[RecycleViewController alloc] initWithNibName:#"RecycleView" bundle:nil];
UIViewController *viewController3 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
UIViewController *viewController4 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3, viewController4, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Each of the windows has the code for the information on the tab.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Home", #"Home");
self.tabBarItem.image = [UIImage imageNamed:#"home"];
}
return self;
}
So far so good, but the RecyclingView (TableView) need a navigation bar where I can set the title using NSLocalizedString.
I would be really grateful for some help.
you need to add a UINavigationController at the second index of your tabBarController's viewControllers instead of adding a view controller -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:#"HomeView" bundle:nil];
UIViewController *viewController2 = [[RecycleViewController alloc] initWithNibName:#"RecycleView" bundle:nil];
UIViewController *viewController3 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
UIViewController *viewController4 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, navController,viewController3, viewController4, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
to set the title at navigation bar you can write this in your RecycleViewController's viewDidLoad
[self.navigationItem setTitle:#"title"];
You can localize the xibs with Interface Builder so you shouldn't have to do it programatically.
http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/
The answer for xcode 4.3.2 is:
alloc the UINavigationBar in RecycleViewController.m as this:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
self.view addSubview:navController.view;
But there is a bug, that navigation bar will display a blank at the top, about 10pixels.
So here is another solution:
use xib builder, add a navigation bar from libary in the top of view RecycleViewController.xib, press ctrl+"navigation Item", point to RecycleViewController.h to insert an IBOutlet,then you will have a good looking navigation bar to show.

Resources