Issues with UINavigationController - objective-c

I am using UINavigationController in my app. While going "Back" I want to skip one view. What I am currently doing is this :
In the viewDidAppear of the view I don't want to see I check to see if it appeared from a back button click, if so I call :
[self.navigationController popViewControllerAnimated:YES];
But what happens is this:
the unwanted view and the previous view get morphed into one view (one on top of the other).
more explanation:
the unwanted view is the 2nd view and I want to go to the rootview:
Another code that I used was:
[self.navigationController popToRootViewControllerAnimation:YES]
The problem with this approach is 2:
a) I still get a back button in the navigation bar that I should not be getting
b) The toolbar items that should be there are not there. (the toolbar itself is there, though!)
Can anyone kindly let me know what I did wrong here ? Thanks.

You can try this:
int count = [self.navigationController.viewControllers count];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:count-2]]
Keep in mind the count is an array starting at 0 so just do count - x, taking that into consideration.
Also I am pretty sure this would work but it doesn't look clean:
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController popViewControllerAnimated:YES];
Hope that helps.

What I needed to do in order to sovle this problem was this ::
Customize the backbutton function from the 3rd view and in the action handler popToRootViewController
Thanks.

Related

Back button not appearing on UINavigationController - can't debug

The back button on a navigation controller is sometimes not appearing. Note that sometimes it does, so there is probably something else going on in som code elsewhere, but I have tried to debug this in every way I can think, and nothing seems to work.
The code for pushing the view controller is as follows (pretty standard):
CommentsTableViewController *vc = [[[CommentsTableViewController alloc] init] autorelease];
vc.puzzleID = self.puzzleModel.puzzleID;
[self.navigationController pushViewController:vc animated:YES];
To debug this problem, I have put the following code in the CommentsTableViewController in viewWillAppear and viewDidAppear (except I only register as an observer once):
self.navigationItem.hidesBackButton = NO;
[self.navigationItem addObserver:self forKeyPath:#"backBarButtonItem" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:nil];
self.navigationItem.backBarButtonItem.title = #"Tactic";
NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setTitle:#"Tactic"];
None of this seems to help. Putting breakpoints in, it seems like the view controller above me in the hierarchy has a title (#"Tactic") and hidesBackButton is already NO. The backBarButtonItem property is nil and is always nil, though I think this is expected behavior even when you do have a backBarButtonItem?
EDIT: In the parent controller, I am setting hidesBackBackButton to YES at first, and then later (before I push on the next controller), setting it back to NO. When I remove these lines, the new view controller has a back button. Why would the navigation item of one view controller affect the navigation item of the next?
Any suggestions or ideas are welcome. Thanks a lot.
Perhaps you might solve the problem debugging it better.You assumed that the button that leads you back is this:
self.navigationItem.backBarButtonItem.title = #"Tactic";
But that's nil, if you want to find that button look in the navigation controller's navigation bar:
NSLog(#"%#",self.navigationController.navigationBar.topItem);
I don't see a reason why your code isn't working, and you also don't need to do this:
self.navigationItem.hidesBackButton = NO;
Try this instead:
self.navigationController.navigationBar.topItem.hidesBackButton = NO;
Anyway this works for me even if I don't set it to NO.
If this doesn't solve the problem it should help at least debugging it, and if you still have that problem post in the comments, and say what does that NSLog() print.
Ok. I've worked it out. I have no idea why this makes a difference, but it does.
In the same run loop where I was calling hidesBackButton, I was also calling:
[self.view addSubview:self.chessBoardViewController.view];
This seemed to be the line that was screwing up the navigationBar somehow. If I remove this line, it works perfectly. So the solution I found was replacing this line with:
[self.view performSelector:#selector(addSubview:) withObject:self.chessBoardViewController.view afterDelay:0];
I dont' know why this worked, so if anyone has any insights on what might be happening, please comment.

How to go back to previous view in Objective-C?

I am a beginner in iOS programming and I want to implement the functionality of going back to home view.
I have use this code:
-(IBAction)onclickhome:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
I have used navigationController in home button click event but it is not jump to home screen.
Do you have any suggestion and source code which applies to my code?
I'm not sure I understand your question.
What do you mean with
I have used navigationController in home button click event but it is
not jump to home screen
?
If you want to pop to the root controller you can use popToRootViewControllerAnimated.
[self.navigationController popToRootViewControllerAnimated:YES];
From Apple doc of UINavigationController
popToRootViewControllerAnimated:
Pops all the view controllers on the stack except the root view controller and updates the display.
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
If you have set up a UINavigationController and its root controller is called A, then if you navigate from A to B and then from B to C you have two possibilities to come back to a previous controller (you can have others but I list the main ones):
navigate back from C to B with popViewControllerAnimated
navigate back from C to A with popToRootViewControllerAnimated
The best way to navigate back and forth views is by pushing and popping views from navigation view controller stack.
To go one view back, use below code. This will ensure that the smooth transition between views are retained.
UINavigationController *navigationController = self.navigationController;
[navigationController popViewControllerAnimated:YES];
To go two views back, do as below
UINavigationController *navigationController = self.navigationController;
[navigationController popViewControllerAnimated:NO];
[navigationController popViewControllerAnimated:YES];
If you aren't going back to your expected view, execute below code before popping any of the views...
UINavigationController *navigationController = self.navigationController;
NSLog(#"Views in hierarchy: %#", [navigationController viewControllers]);
You should see an array like the below one, which will help you in verifying the stack is maintained as expected.
Views in hierarchy: (
"<Main_ViewController: 0x1769a3b0>",
"<First_ViewController: 0x176a5610>",
"<Second_ViewController: 0x176af180>"
I use
[self dismissViewControllerAnimated:YES completion:nil];
since others answers didn't worked in Xcode 8.0

Navigation from UIViewController to UITableViewController

I am trying to move to UITableView named Cart from UIView by clicking on right bar button item , the button action event i wrote
Cart *crtObj=[[Cart alloc]initWithNibName:#"Cart" bundle:nil];
[self.navigationController presentModalViewController:crtObj animated:YES];
[crtObj release];
And from the same Bar by clicking on left bar button i am navigating previous tableView which works properly, the code for the same is
self dismissModalViewControllerAnimated:YES];
I tried a lot please suggest something?
//[self.navigationController presentModalViewController:crtObj animated:YES];
//presentModalViewController
try this
[self.navigationController pushViewController:crtObj animated:YES]
I wonder why u have to use [self.navigationController presentModalViewController]
but not [self.navigationController pushViewController:crtObj animated:YES].
They are different.
[self.navigationController presentModalViewController:ControllerA] will simply pull up a view of ControllerA from the bottom of ur screen. ControllerA will not be related the controllers those u are navigating along. (I am not sure if this method works)
[self.navigationController pushViewController:ControllerA animated:YES] will push ControllerA to the navigationController and it's stored with the controllers those u are navigating along in sequence.
i suggest using the second one because the structure of ur apps will be simpler.
but if u still want to use the first way. Maybe you can try like this:
Assume u have now navigated to ControllerA, and wanna show up Controller B
call [ControllerA presentModalViewController:ControllerB] instead

Loading another view from table selection without losing navigation (back)

I have an app that is controled through a UITabBar. In one of the sections within the tab bar, I have a navigation table. It works fine from an example I did from one of the books but I want to be able to go another view controller (aka another xib file) when the user selects a row and I want the user to be able to go back easily. I realize this has to do with pushingViewControllers but I am stuck. Here is where I think the problem is. my code is at the bottom. If you notice, I commented out
// [self presentModalViewController:flowerDetailViewController animated:YES];
While this did take me to the my flowerDetailViewController XIB file, I lost the ability to do navigation (go back). If anyone has any ideas or suggestions, it would be much appreciated.
Thank you
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
FlowerDetailViewController *flowerDetailViewController =
[[FlowerDetailViewController alloc] initWithNibName:
#"FlowerDetailViewController" bundle:nil];
/*flowerDetailViewController.detailURL=
[[NSURL alloc] initWithString:
[[[flowerData objectAtIndex:indexPath.section] objectAtIndex:
indexPath.row] objectForKey:#"url"]];*/
flowerDetailViewController.title=
[[[flowerData objectAtIndex:indexPath.section] objectAtIndex:
indexPath.row] objectForKey:#"name"];
//[self presentModalViewController:flowerDetailViewController animated:YES];
[self.navigationController pushViewController:
flowerDetailViewController animated:YES];
[flowerDetailViewController release];
}
It doesn't make any sense to execute these two methods on the same view consecutively.
[self presentModalViewController:flowerDetailViewController animated:YES];
..
[self.navigationController pushViewController: flowerDetailViewController animated:YES];
because they give a very different result, from user experience point of view.
The first one presents a view modaly. Modaly means, that he application brings up the view to the top (imagine a Z-axis, when you hold the phone, it is a line from the phone to you, on top of it means closer to you), and the user is stuck in that view exclusively because it is on the top, he/she cannot touch anything else from the application unless he resolves the options presented in the view and the view goes away.
The second method is pushing the view onto he stack of views that all belong to the navigation controller. The navigation controller pushes views onto the screen like you would lay a stack of cards onto the table, card1, put onto that card 2, put onto that card 3...and so on card N. But you still have the ability to touch other options that are all around the navigation controller.To get back to the card 1, you need to remove card(views) that are on top of it, for removing on-top views, the navigation controller provides the back button automatically.
Only you cann tell, which of these two is handy in terms of your application UI and design.

pushViewController iphone not working

i am unable to get the pushViewController to work on a View Based Application on the iPhone. On my 'ProjectViewController' i have a IBAction with the following code :
-(IBAction)switchAugmented
{
ARViewController *viewController = [[ARViewController alloc] initWithDelegate:self];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
}
When i run the program and press ibaction nothing happens. Besides that statement above do i need to do anything else to make the view appear? what am i missing?
(...) on a View Based Application (...)
You just have no UINavController! Try to embed your main view in UINavigationController and everything will start working.
Double check to make sure that the button you are pressing is connected to the right method in interface builder. Also try putting an NSLog statement in the switchAugmented to see if the method is getting called.
You also have to check and see if you have a UINavigationController instance, otherwise you won't be able to push a new view controller.
You won't be able to push a new view controller in a View Based Project. You need to create a Navigation Based Project or add an instance of the UINavigationController in your Main.nib (if your using a nib file) only then will the push view controller will work