Search code examples
iphonexcode4uinavigationcontroller

how to return to previous form using UINavigation Controller


I have used UInavigation controller when the calling form is a UITable but in this case I've used a button to call the form I need using the following code -

EditCodesController *editcodesController = [[EditCodesController alloc] initWithNibName:@"EditCodesController" bundle:nil];
UINavigationController *mySocondView =[[UINavigationController alloc]
                                       initWithRootViewController:editcodesController];
[self presentModalViewController: mySocondView animated:YES];

This works ok and I've place a 'Back' button on the navigator bar on the form called. What code do I need to use to return to the original form?


Solution

  • You need to call dismissModalViewControllerAnimated: on the Navigation Bar to dismiss your Modal View Controller. What you need to do is have a selector method in your class where the back button is displayed which calls the dismissModalViewControllerAnimated: selector on the original navigation controller

    In your opened view, you could have the button set up and the selector like so:

     // In viewDidLoad (or similar)
     UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack)];
     self.navigationItem.leftBarButtonItem = back;
    
     // Your goBack Selector will then be
     - (void)goBack {
         [self dismissModalViewControllerAnimated:YES];
     }