What method will be called when Navigation back button is clicked -IOS? - iphone

This questions look repeated, but I din't got proper solution for my problem. In my case, Once I clicked back button I want to hide the navigation bar. For e.g. View1 pushed view2, view2 will have navigation bar,once I clicked back it brings me back to view 1 it should not contain navigation bar I need to hide it. I tried with viewWillDisappear method in view 2 to hide, it worked but if I have more view and I'm pushing each view from view1 if I click back button,navigation bar should hide in view1. So is anyway is there to know in view 1 itself that other view is popped. I tried viewwillappear method in view1 its not called. What can I do here.?
This method got called once I initially load view1, not after popping view2..
- (void) viewWillAppear : (BOOL)animated
{
[root_obj.navigationController setNavigationBarHidden:YES animated:YES];
}
This is how I am pushing view 2..
[root_obj.navigationController pushViewController:view2 animated:NO];
thanks in Advance

i think, u have forgot to call viewWillAppear: method of super class.
-(void) viewWillAppear : (BOOL)animated
{
[super viewWillAppear:animated];
[root_obj.navigationController setNavigationBarHidden:YES animated:YES];
}

Why don't you hide the NavigationBar in View1's ViewDidAppear method
Edit
I am not sure what the problem with your code but you can do these kind of things in ViewWillAppear or ViewDidAppear methods for more information UIViewController Class Reference and check ViewWillAppear or ViewDidAppear

Related

How to hide navigation bar in iPhone?

Currently i am working in iPhone app, I have two screen like A and B, A has no navigation bar, but B has navigation bar. so i set like this.
Class A:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=#"A";
[self.navigationController setNavigationBarHidden:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES];
}
Class B:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=#"B";
[self.navigationController setNavigationBarHidden:NO];
}
-(void)Previousscreen
{
[self.navigationController popViewControllerAnimated:YES];
}
then i run the application, When i go to previous class like B to A at the time blue color show in B class below attached screen shot for your reference. How to fix this issue? please help me
Thanks in Advance
Set it in class B
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO];
}
You'll need to use this code:
[navigationController setNavigationBarHidden: YES animated:YES]
in - (void)viewWillAppear:(BOOL)animated or later in the view lifecycle in both classes. [Avoid doing this in - (void)viewDidLoad.]
The trick here is in using the setNavigationBarHidden:animated: method (in place of the simpler setNavigationBarHidden: method). This will ensure your UI issue goes away and also any positional issues due to it.
P.S. Check the value of self.navigationController.navigationBarHidden (instead of self.navigationController.navigationBar.hidden) if you need to check if your navigation bar is hidden, at some point, in your code.
I don't think a behavior when you are hiding and showing the navigation bar dynamically as you are pushing controllers is supported.
Simple solution - hide the animation bar of the UINavigationController and if you want to show it on some controller, just add a UINavigationBar to it.
Use below line to hide navigationBarin viewWillAppear: method -
-(void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden=YES;
}
Try setting the navigationBarHidden: in viewWillDisAppear of class B
in class B
-(void)viewWillDisAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES];
}
Your code is okay for Hide and Unhide the navigationBar. The problem is that you're hiding Class A's navigationBar in viewWillAppear: which is called just before appearing the view so before loading the Class A view navigationBar is being hidden each time.
And if we talk about your blue color i think it is your window color. Because after hidden the navigationBar there will be a space above your self.view which height is 44.0. So there are three options to fixed it.
Hide Class A's navigationBar in Class A's viewDidAppear: method.
Set your window color what you want to show.
You can add an image to window background in which at top of image make a navigationBar same as Class B's navigationBar so when the original navigationBar will be hide it will see.
I've had to solve this recently and I found that it was necessary to call setNavigationBarHidden:NO immediately after pushViewController: and setNavigationBarHidden:YES immediately after popViewController:, with animated YES in each call.
So, when pushing:
[nc pushViewController:classBView animated:YES]
[nc setNavigationBarHidden:NO animated:YES]
and when popping:
[nc popViewControllerAnimated:YES]
[nc setNavigationBarHidden:YES animated:YES]
But in my case, while I could do pushing as above, I didn't want to alter my class B and instead wanted it to not know of care that the navigation bar wasn't previously hidden (since its not my code). Also, that view gets popped using the normal Back button, there was no explicit call to popViewControllerAnimated:. What was going to work best in my code was to make my class A be the UINavigationController delegate and hide the toolbar on a delegate method call when the pop occurs.
Unfortunately I found that the UINavigationControllerDelegate methods weren't too helpful, willShowViewController & didShowViewController are called indistinguishably when pushing my class B view or when popping back to it from another one that it has pushed.
I followed a suggestion in https://stackoverflow.com/questions/642312/ about overriding UINavigationController and I made some custom delegate methods, one is called right after [super popViewControllerAnimated:]. My subclass is available at https://gist.github.com/jpmhouston/6118713 and delegate method is:
- (void)navigationController:(UINavigationController *)navigationController isPoppingViewController:(UIViewController *)poppedViewController backTo:(UIViewController *)revealedViewController {
if (revealedViewController == self && [poppedViewController isKindOfClass:[MyClassB class]]) {
[navigationController setNavigationBarHidden:YES animated:YES];
// ...and more code to run only when going from class B back to class A
}
}
I'm sure there are simpler ways to have setNavigationBarHidden: called following the Back button being pressed, but this worked for me.

Dismiss two modal (table)view controllers

I know there's like 3-5 similar questions here, but non of the answers solves my problem.
I have a ViewController that opens a modal (table)view controller, which opens another one. Both modal view controllers are in fact table view controllers. I'm trying to dismiss both of them from the second one. I tried every accepted answer on similar question, none of them worked for me.
I tried
[self dismissModalViewControllerAnimated:true]
[self.parentViewController dismissModalViewControllerAnimated:true]
[self.parentViewController.parentViewController dismissModalViewControllerAnimated:true]
[self.presentingViewController dismissModalViewControllerAnimated:true]
[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:true]
When I try options 2, 3 and 5, nothing happens at all. When I use options 1, and 4, I see dismiss modal view animation and the underlying view itself for a moment, and then everything goes back to the second modal view (this time without animation).
I'm starting to think that this have something with the fact that I use tableViewControllers for modal views.
Btw, I'm dismissing modal views in didSelectRowAtIndexPath.
Try this:-
When you dismiss your SecondView set a BOOL flag variable in app delegate file and check that variable in your FirstView's viewWillAppear method whether SecondView was open and close or not. If so, then [self dismissModalViewControllerAnimated:true]
typical model view controller behavior would suggest that you dismiss the modal view controller from the calling view controller rather than from self. not a hard and fast rule, but good practice.
to accomplish this, create a protocol:
#protocol MyModalViewControllerDelegate
- (void)modalViewControllerDidFinish;
#end
and make both the parentViewController and FirstModalViewController be implemntors of this protocol.
#interface FirstModalViewController <MyModalViewControllerDelegate>
then in both FirstModalViewController.h and SecondModalViewController.h, add:
#property id<MyModalViewControllerDelegate> modalViewControllerDelegate
in both parentViewController and FirstModalViewController, right before calling presentModalViewController:... , set the following:
modalViewControllerAboutToAppear.modalViewControllerDelegate = self;
[self presentModalViewController:modalViewControllerAboutToAppear animated:YES];
next, in the SecondModalViewController, in the code where you determine that the item needs to be dismissed, call
[self.modalViewControllerDelegate modalViewControllerDidFinish];
now, in FirstModalViewController, implement the following:
- (void)modalViewControllerDidFinish:(MyModalViewController*)controller {
[self dismissModalViewControllerAnimated:YES]
[self.modalViewControllerDelegate modalViewControllerDidFinish];
}
and finally, in the parent view controller, you should be able to perform:
- (void)modalViewControllerDidFinish:(MyModalViewController*)controller {
[self dismissModalViewControllerAnimated:YES]
}
Since I don't use delegate files, I did the following:
To FirstView add field
BOOL mClose;
To FirstView add method
- (void)close
{
mClose = YES;
}
To FirstView method viewDidAppear add
if (mClose)
{
[self dismissModalViewControllerAnimated:YES];
}
To FirstView method which opens SecondView add
[secondView closeWhenDone:self];
To SecondView add field
FirstView *mParent;
To SecondView add method
- (void)closeWhenDone:(FirstView*)parent
{
mParent = parent;
}
To SecondView method which closes it add
[mParent close];

how will back button work in iPhone whithout using navigation bar?

#
I have two view named FirstView and Second View. Now I have one button in SecondView. If i click this button then i want to move FirstView. What is the code for it?
I don't want Navigation Bar.
And My button should work as a "Back" Button.
#
You could still use a UINavigationController, but hiding the navigation bar with calling
[self.navigationController setNavigationBarHidden:true animated:false];
in the second view's viewDidLoad method.
Then just show the second view by pushing it on the navigation stack in the first view:
[self.navigationController pushViewController:secondViewController animated:true];
and pop it from the stack when your button is pressed in the second view:
[self.navigationController popViewControllerAnimated:true];
Add SecondView as subview of FirstView. To go back, in the action method of back button, you could remove it from parent view.
In firstView :
[self addSubview:secondView];
In secondView :
- (IBAction)backButtonClicked:(id)sender {
[self removeFromSuperview];
}

Reload button on navigation controller on window.xib

heres my issue, I have a tabbar as the root controller, I then have tabs which are navigation controllers. ( all this is done in Interface Builder )
each navigation controller has a view controller. one of these view controllers have a table view.
on the navigation controller of this table view, I want a item button which reloads the table view.
i know to use reloadData.
I have linked the item button up to a IBAction in the view controller of the navigation controller by making a new NSObject in the mainWindow.xib pointing to this view controller. in the view controller I have this IBAction
-(IBAction)reloadTable:(id)sender
{
NSLog(#"RELOADDDDING");
[self.tableViewIB reloadData];
}
The NSlog is displayed, but the table is not reloading.
I have tried with and without the self.
I had the exact same setup with the exact same issue. Below is how I fixed it. Put this code in the view controller with the Table.
- (void)viewWillAppear:(BOOL)animated {
//reload table now that we have a good array to show
[self.view reloadData];
[super viewWillAppear:animated];
}
Updated in response to your comment
Add this code to the action of your button being pushed
//button was pushed code
TableViewController * vc = (TableViewController *)[[TableViewController alloc] init];
//this tableview should be named whatever you have your table named as.
[vc.tableView reloadData];
[vc release];
I had asked the exact same question few minutes back - - - This might be a simple question. HELP with Resetting UITableViewCells
Just do this !
Assign TableView as a property.
Reload it in ViewWillAppear

How to hide/disable only the first uinavigationbar?

I've been wandering how to hide / remove / disable only the main or first navigation bar in the navigation controller so that I could put an image as a whole background screen but I couldn't find any solution.
Did try hide the titleview in viewdidLoad of the main navigation controller but didn't work. Tried using navigationBarHidden but it hides the whole navigation bar for the next stack of controller.
So, I'm not sure how to do this. To give you an example, I would like to have something like this app - The Masters Golf Tournament - http://appshopper.com/sports/the-masters-golf-tournament.
If you look at Screen 1, it doesn't have any nav bar at the top but when you touch any options it will push to a new view controller and have the nav bar appear as in Screen 3,4 and 5.
Hope anyone could help me with this.Thanks a lot!
In most of my applications I have a custom UIViewController class that I derive all other custom controllers from. In some of these, I added a method like navigationBarInitiallyHidden to the base class that other classes can override. The default result depends on the nature of the application.
In the delegate of the navigation controller, when a controller is being shown that implements that method, the delegate hides or shows the navigation controller accordingly. Since I animate the hide or show, I check the current state and do nothing if no change is needed.
You could do something simpler in your delegate method. If the controller being shown is the root controller, hide the navigation bar, otherwise show it if it is hidden.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ( viewController == rootController ) {
[navigationController setNavigationBarHidden:YES animated:animated];
} else if ( [navigationController isNavigationBarHidden] ) {
[navigationController setNavigationBarHidden:NO animated:animated];
}
}
You can hide the navigation bar:
[self.navigationController setNavigationBarHidden:YES];
and where you want to show the navigation bar again
[self.navigationController setNavigationBarHidden:NO];
hide/disable
self.navigationController.navigationBarHidden = YES;
show/Enable
self.navigationController.navigationBarHidden = NO;
You can hide navigation bar by using bellow code. Below code will hide navigationbar at the time of viewWillAppear.
Objective C
-(void)viewWillAppear:(BOOL)animated
{
[[self navigationController] setNavigationBarHidden:YES animated:NO];
}
Swift
self.navigationController?.setNavigationBarHidden(true, animated: animated)