How to trigger navigationController:willShowViewController delegate method in AppDelegate - objective-c

How can I trigger the navigationController:willShowViewController delegate method for my implementation below so that all the view controllers in the navigation controller will conform to the colorWithHexString #faf6f5?
Currently, my FirstViewController will be displayed but it doesn't seem to call the delegate method to change the color of it's navigation bar (as well as for all other view controllers that are stacked onto the navigation controller subsequently). Note that I have already added the "UINavigationControllerDelegate" to my app delegate header file.
//In App Delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Set First View
FirstViewController *firstView = [[FirstViewController alloc]init];
// pushes a nav con
UINavigationController *tempNavcon = [[UINavigationController alloc]initWithRootViewController:firstView];
self.navcon = tempNavcon;
[self.window addSubview:navcon.view];
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
navigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"#faf6f5"];
}

is there a reason why you are trying to change the tintColor in an event method rather than when the UINavigationBar instance is created?

Here's how you do it. (Note that UIColor doesn't accept hex values; you should use an RGB value, or check this page.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Initialize your view controller.
FirstViewController * firstView = [[FirstViewController alloc] init];
// Create an instance of a UINavigationController. Its stack contains only firstView.
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:firstView];
//Here is where you set the color of the navigationBar. See my note above for using RGB.
navController.navigationBar.tintColor = [UIColor greenColor];
// You can now release the firstView here, navController will retain it
[firstView release];
// Place navigation controller's view in the window hierarchy
[[self window] setRootViewController:navController];
[navController release];
[self.window makeKeyAndVisible];
return YES;
}

Related

UINavigationController is null after going back

I'm having troubles with an UINavigationController which viewControllers property is null after tapping Back on a previously pushed view controller.
The root view controller in AppDelegate is defined with that code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
StartViewController *startVc = [[StartViewController alloc] initWithNibName:#"StartViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:startVc];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
At StartViewController I'm hiding the navigation bar within - (void)viewWillAppear:(BOOL)animated where I've also a UIButton instance where I push the next view controller when tapping with that code:
- (IBAction)buttonTapped:(id)sender {
PhilosophyViewController *philsophyVc = [[PhilosophyViewController alloc] initWithNibName:#"PhilosophyViewController" bundle:nil];
[self.navigationController pushViewController:philsophyVc animated:YES];
}
I'm heading over to the next view as expected. But when tapping the Back button at the navigation bar, the previous view (StartViewController) is empty and when checking the viewControllers property of the navigation controller - it is null?
Any idea?
Thanks in advance.
The back button of the pushed viewController can be hooked up to an IBAction
-(IBAction)backButtonPressed:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
This should pop you back to the previous view controller front the stack.
Hope this helps.

ios remove tabbar on button touch

I've a tab bar based project created and added a login view prior to tab view like this.
AppDelgate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
loginView = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
loginView.delegate = self;
[window addSubview:loginView.view];
[self.window makeKeyAndVisible];
return YES;
}
-(void) loginViewControllerDidFinish:(LoginViewController *) loginViewController{
[window addSubview:tabBarController.view];
}
I'm calling loginViewControllerDidFinish: after authentication in LoginViewController as
[self.delegate loginViewControllerDidFinish:self];
Now in one of the tabs i've added logout button and want to come back to loginView on touch.
-(IBAction) logout:(id)sender{
NMSAppDelegate *appDelegate = (NMSAppDelegate *)[[UIApplication sharedApplication] delegate];
//[appDelegate.tabBarController release];
[appDelegate.tabBarController.view removeFromSuperview];
}
it shows the loginView after calling the above method but after re-login it shows all the previous data and previously selected tab but i want it to be afresh from the first tab.Is there anyway to completely delete the tabbar on logout.so that i'm starting the new session.
Thanks
Cant you create another instance of appDelegate.tabBarController ?
Something like this:
appDelegate.tabBarController = [[UITabBarController alloc] init];
appDelegate.tabBarController.viewControllers = [NSArray arrayWithObject:VIEW_CONTROLLER1, VIEW_CONTROLLER2,nil];
appDelegate.window.rootViewController = tabBarController;
[appDelegate.window makeKeyAndVisible];

Unable to display tableView in iOS app

I am fairly new to iOS and am attempting to display a tableView controller from another tableView controller in a drill down routine for an iPad app. However, the new tableView will not display. I can follow the program logic through the following routine in debug mode but after this logic, the same view remains on the screen. I set breakpoints in the new tableview program to be displayed and they are never reached. I have included the HEDView.h in the application file for this program and have no clue why the new view is not displayed. Any help is or suggestions for more info is appreciated.
Here is the routine to call the tableView: HEDView will not display.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
[tableView deselectRowAtIndexPath:indexPath animated:NO];
HEDView *detailViewController = [[HEDView alloc] initWithNibName:#"HEDView" bundle:nil];
// Pass the selected object to the new view controller.
detailViewController.title = #"HEDView";
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
If your requirement is to navigate to other viewcontroller when cell in the row is selected, then I think your navigationcontroller is not properly allocated.While debugging check whether self.navigationController is returning proper address.If not then you have to first properly allocate it.
and one more thing, HEDView is UIViewController so you should follow proper naming convention.
Implement - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in AppDelegate.m, and also include #property (nonatomic, retain) UINavigationController *navControl; in AppDelegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:[navControl view]];
[self.window makeKeyAndVisible];
return YES;
}
I think it will be helpful to you.

BarButtonItem does not show up

I created a new window based application, and a RootViewController that sublcasses UIViewController. I created a UINavigationController in the AppDelegate and when I add the UINavigationController's view to the window, I do see the navigation bar on top. However, I cannot seem to add a button to the navigation bar - there are no compile errors, but the button (and the title) does not show up. The code is below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RootViewController * rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleDone target:nil action:nil];
navigationController.title = #"Foo";
navigationController.navigationItem.rightBarButtonItem = next;
[self.window addSubview:navigationController.view];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
What am I doing wrong?
Change this:
navigationController.navigationItem.rightBarButtonItem = next;
to this:
rootViewController.navigationItem.rightBarButtonItem = next;
May be style:UIBarButtonItemStyleDone only used in editing mode is YES. If in Root controller or sub view controller, you may try call self.editing = YES, but in AppDelegate you can't.

Tab bar, reload every time tab is pressed

I am creating an app in which I have five tabs. I need to reload each controller every time when tab is pressed.
Put the code you want to reload, in the view will appear or in view did appear of all the view.
All the best.
Example:
// AppDelegate ( and <UITabBarControllerDelegate> )
// Creation tabbar and controllers
UIViewController* myController1 = [[UIViewController alloc] init] autorelease];
UINavigationController* nav1 = [[[UINavigationController alloc] initWithRootViewController:myController1] autorelease];
UIViewController* myController2 = [[UIViewController alloc] init] autorelease];
UINavigationController* nav2 = [[[UINavigationController alloc] initWithRootViewController:myController2] autorelease];
NSArray *array = [NSArray arrayWithObjects: myController1, myController2, nil];
UITabBarController* tab = [[[UITabBarController alloc] init] autorelease];
tab.viewControllers = array;
tab.delegate = self; // <-- don't forget set delegate for TabBarController
// TabBarController Delegate methods
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
{
// Reload selected VC's view
[viewController.view setNeedsDisplay];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//other codes
[self.tabBarController setDelegate:self]
//other codes
}
// UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController respondsToSelector:#selector(reloadDataTemp)]) {
[(YourViewController *)viewController reloadData];
}
}
So write a method to redraw the elements on your page and call it on tab press. I will edit this post if you provide more information on the problem you are facing.
if you are you are using the uitableview use this
[tableview reloaddata];
I hope you are talking about the webview the webview should reload every time a tabbar item is navigated well just implement [webview reload] in the tab bar delegate.
Swift 5
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let vc = self.viewControllers?[1] as? stepVC // ViewController That need to be loaded
vc?.viewDidLoad()
}