Navigation Bar hiding when moving to new view controller - ios

When I am trying to move from one view controller to other by pressing on a table view cell in the present view controller. The problem is when I do this :
FGTipViewController *vc = [[FGTipViewController alloc] initWithNibName:#"FGTipViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
It takes me to the new view controller but the navigation bar disappears. This happens for a particular view controller only and works fine for others. I am not able to make out any elementary difference between these classes. What could be the cause of this kind of problem? Let me know if more details are required.

Try these lines when you push your view,
FGTipViewController *vc = [[FGTipViewController alloc] initWithNibName:#"FGTipViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
And also in the viewWillAppear method of FPTipViewController,
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}

In FGTipViewController,
-(void) viewWillAppear:(BOOL) animated {
[super viewWillAppear];
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}

I did this and it works. Thanks!
UINavigationBar* navBar = self.navigationController.navigationBar;
if (navBar) {
[UIView animateWithDuration:ANIMATION_DURATION animations:^{
[navBar setFrame:CGRectMake(0, 20, navBar.frame.size.width, navBar.frame.size.height)];
}];
}

Related

Push view controller not working

I'm using UINavigation and UIPageView controller in a UIViewController. I have 3 buttons and here is my code :
- (IBAction)StartButtonClicked:(id)sender {
NSLog(#"start button pressed");
[_Timer invalidate];
_Timer = nil;
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
CardViewController* controller = [storyboard instantiateViewControllerWithIdentifier:#"CardViewController"];
[self.navigationController pushViewController:controller animated:NO];
}
}
- (void)pageViewController {
self.pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pvc.view.frame = self.view.bounds;
[self.view addSubview:self.pvc.view];
[self.pvc setViewControllers:#[[viewControllerArray1 objectAtIndex:0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
- (IBAction)rightButtonPressed:(id)sender {
[self pageViewController];
[self.pvc setViewControllers:#[[viewControllerArray1 objectAtIndex:1]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
- (IBAction)leftButtonPressed:(id)sender {
[self pageViewController];
[self.pvc setViewControllers:#[[viewControllerArray1 objectAtIndex:2]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}
When I click on right button and came back to this view controller my start button's push view not working.
Please check the answer .
iOS - Push viewController from code and storyboard
Don't initialize Storyboard always,
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
You can set the rootViewController from the UtoryBoard itself.
Set initialViewController as a UINavigationController and Set the FirstViewController as RootViewController.
Edit :
For Push use the below code,
CardViewController* controller = [storyboard instantiateViewControllerWithIdentifier:#"CardViewController"];
[self.navigationController pushViewController:controller animated:NO];
and Back. If use the default backButton not need to write any code.
If use are using custom backButton,
[self.navigationController popViewController];
Check 2 things,
Your storyboard identifier is correct.
The root view have navigation Controller.
Your StoryBoard should be like this,

Switching back and forth in views

very new to obj-c.
I´ve made a simple MainViewController, loading its view from a xib. (From a standard single view template)
On the main view is a button that takes me to another view. Below is the Method:
-(IBAction)forward:(id)sender
{
tvc = [[TrackViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
[self.view addSubview:tvc.view];
}
On the other view (also loaded from xib) i have a button to take me back to the main view.
Here is my IBAction for returning to the main view:
-(IBAction) back:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
However it does not return to the main view. Not with popToRootViewControllerAnimated: either.
Any response will be appreciated!
This answer applies when you are not using UINavigationBar
In your Forward button you need to write.
TrackViewController *lf = [[TrackViewController alloc]initWithNibName:#"TrackViewController" bundle:nil];
[self presentViewController:TVC animated:YES completion:nil];
and in the Backward screen you should right.
[self dismissViewControllerAnimated:NO completion:nil];
You should load your MainViewController in your AppDelegate in an UINavigationController
MainViewController *mainVC = ...
UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = naviVC;
Then you can go forward in your MainViewController:
-(IBAction)forward:(id)sender;
{
tvc = [[TrackViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:tvc animated:YES];
}
and go back in your other ViewController:
-(IBAction) back:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
If your MainViewController is inside a UINavigationController, then the forward method should look like this:
-(IBAction)forward:(id)sender
{
tvc = [[TrackViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:tvc animated:TRUE];
}

Shift to Parent View Controller from child view controller - iOS

I have a main Actions View Controller, and on that there is button "Review". Review Button's functionality is :
- (IBAction)btnReview:(id)sender
{
ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:#"ReviewViewController" bundle:nil];
[self addChildViewController:vc];
[vc didMoveToParentViewController:self];
[self.view addSubview:vc.view];
}
Now on Review Page, I have a button. And on that button's action, I want to switch back to Parent View Controller. Its view should be displayed. I have tried following code, it either pauses or crashes application.
[self didMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
I even tried:
[self dismissModalViewControllerAnimated:YES]
I have read other posts related to this question as well but could not find a satisfactory answer. Please Help!
I'm assuming that this is a custom viewcontroller container you're trying to build, "self" is the Main Actions viewController you're talking about.
Try this:
- (IBAction)btnReview:(id)sender
{
ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:#"ReviewViewController" bundle:nil];
[self addChildViewController:vc];
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
vc = nil;
}
For the "Back" button, I'm assuming this is the Button to return to Main Actions viewController from the Review viewController, try this:
- (IBAction)btnReviewHide:(id)sender
{
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
}
Hope this helps.
You should use delegation. When you click the button in your ReviewViewController, you call a method like: [self.delegate hideReviewController:self];
And this method would look something like:
- (void)hideReviewController:(ReviewViewController *)controller {
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
controller.view.alpha = 0;
} completion:^(BOOL finished) {
[self.view removeSubview:controller.view];
// If you want the Review Controller to be deallocated:
[self removeChildViewController:controller];
}];
}
EDIT:
In your ReviewDelegate.h file add:
#class ReviewViewController;
#protocol ReviewDelegate <NSObject>
- (void)hideReviewController:(ReviewViewController *)controller;
#end
Then add this property:
#property (nonatomic, weak) id <ReviewDelegate> delegate;
In the parent class:
- (IBAction)btnReview:(id)sender
{
ReviewViewController *vc = [[ReviewViewController alloc]initWithNibName:#"ReviewViewController" bundle:nil];
vc.delegate = self;
[self addChildViewController:vc];
[vc didMoveToParentViewController:self];
[self.view addSubview:vc.view];
}
- (void)hideReviewController:(ReviewViewController *)controller {
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
controller.view.alpha = 0;
} completion:^(BOOL finished) {
[self.view removeSubview:controller.view];
// If you want the Review Controller to be deallocated:
[self removeChildViewController:controller];
}];
}
I also suggest that you read about delegation
Try this
self.view=nil;
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
You don't need to make it complex... Just use UINavigationController.
To navigate from ParentVC to ChildVC :
ChildViewController *ChildVC = [[ChildViewController alloc]initWithNibName:#"ChildViewController" bundle:nil];
[self.navigationController pushViewController:ChildVC animated:YES];
And for navigating back from ChildVC to ParentVC :
ParentViewController *ParentVC = [[ParentViewController alloc]initWithNibName:#"ParentViewController" bundle:nil];
[self.navigationController popViewControllerAnimated:YES];
Since you are adding the Child View as a Subview on to Parent you need to manually it from its superview (in your case parentview). Rather then adding it as a subview you can make use of presentViewController method as follow:
- (IBAction)btnReview:(id)sender
{
ReviewViewController *vc = [[ReviewViewController alloc] initWithNibName:#"ReviewViewController" bundle:nil];
[self presentViewController:vc
animated:YES
completion:nil];
}
and in the child class code for back button will be :
-(IBAction)backButtonClicked:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Sounds like what you're looking for could be taken care of with a UINavigationController. Just use pushViewController: to push the new view onto the screen. Then, a back button will appear in the navigation bar atop the view, which can be used to return to the "parent" view controller.
See the documentation.
writing only
- (IBAction)backbutton:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
worked in my case

UINavigationController, how to hide tabbar in second level viewController then show tabbar in third level viewController

here is a piece of my code, but in this way, when I push the third level view controller, the tabbar won't show.
//at first level
SecondLevelViewController *_2vc = [[SecondLevelViewController alloc]initWithNibName:#"SecondLevelViewController" bundle:nil];
_2vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:_2vc animated:YES];
//at second level
ThirdLevelViewController *_3vc = [[ThirdLevelViewController alloc]initWithNibName:#"ThirdLevelViewController" bundle:nil];
_3vc.hidesBottomBarWhenPushed = NO;
[self.navigationController pushViewController:_3vc animated:YES];
// Load the view
AddViewController *aController = [[AddViewController alloc] init];
// Set the view title
aController.title = #"Add View";
// hide tabbar
aController.hidesBottomBarWhenPushed = YES;
// add it to stack.
[[self navigationController] pushViewController:aController animated:YES];
-(void)viewWillAppear: (BOOL)animated
{
[super viewWillAppear:animated];
[self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear: (BOOL)animated
{
[super viewWillDisappear:animated];
[self.tabBarController.tabBar setHidden:NO];
}
Instead of setting the values of hidesBottomBarWhenPushed when you initialise the view controllers, you should instead handle the hiding mechanism in the -(void)viewWillAppear:(BOOL)animated in the view controllers instead.
An example of this implementation would be:
In SecondLevelViewController.m
-(void)viewWillAppear:(BOOL)animated
{
[_bottomBar setHidden:YES];
}
In ThirdLevelViewController.m
-(void)viewWillAppear:(BOOL)animated
{
[_bottomBar setHidden:NO];
}

push uiviewcontroller from presentModalViewController

I Show a view using presentModalViewController. and from this UIView I want to push a UIView using UINavigationController.
I tried below code for this
[self.parentViewController.navigationController
pushViewController:objViewFullScreen
animated:YES];
But it did not works for me. so please can any one suggest how I push a view from ModelViewController.
Thanks
First you have to present your modal view controller inside a navigation controller:
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyNib" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentModalViewController:nc animated:YES];
[vc release];
[nc release];
Then inside MyViewController you can do:
OtherViewController *vc = [[OtherViewController alloc] initWithNibName:#"MyOtherNib" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
-(void)pushViewControllerWithCustomAnimation:(UIViewController *)newViewController {
newViewController.view.alpha = 0.0f;
[self.view addSubview:newViewController.view];
[UIView animateWithDuration:1
animations:^{
newViewController.view.alpha = 1;
}
completion:^(BOOL fin){
if (fin) {
// finally display the new viewcontroller for real
[self.navigationController pushViewController:newViewController animated:NO];
}
}];
}

Resources