Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 610
user1490535 Avatar asked Jul 09 '12 09:07

user1490535


People also ask

How do you create a view in Objective C?

Create new “View” file from the User Interface menu. This will be your xib that represents a person in your app (let's say it's a dating app). Call it “PersonXIB” (or whatever you want). You're going to see what looks very much like a single view storyboard file with a blank view.

How do I present a view controller in Objective C?

Set the modalPresentationStyle property of the new view controller to the desired presentation style. Set the modalTransitionStyle property of the view controller to the desired animation style. Call the presentViewController:animated:completion: method of the current view controller.


2 Answers

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
like image 128
Lorenzo B Avatar answered Oct 23 '22 15:10

Lorenzo B


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>"
like image 23
user2734323 Avatar answered Oct 23 '22 15:10

user2734323