Make back button go to the previous view programmatically

Colton Anglin

I have a UIBarButtonItem and would like to programmatically set the action that goes to the previous controller (in my case, my previous view is a UITableViewController).

Below is my code that I am currently using to make the bar button item although the button doesn't go to the previous view yet.

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                style:UIBarButtonItemStyleDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Website"];
item.leftBarButtonItem = leftButton;
item.hidesBackButton = YES;
[myBar pushNavigationItem:item animated:NO];
larva

Add following code in your controller, in - (void)viewDidLoad function:

call [self addBackButtonWithTitle:@"back"]; if You want to custom backbutton with title.

or [self addBackButtonWithImageName:@"back_button_image_name"]; if You want custom backbutton with image.

/**
 *  @brief set lef bar button with custom title
 */
- (void)addBackButtonWithTitle:(NSString *)title {
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed:)];
    self.navigationItem.leftBarButtonItem = backButton;
}

/**
 *  @brief set left bar button with custom image (or custom view)
 */
- (void)addBackButtonWithImageName:(NSString *)imageName {
    // init your custom button, or your custom view
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 40, 22); // custom frame
    [backButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    // set left barButtonItem with custom view
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

- (void)backButtonPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Make back button go to the previous view programmatically

From Dev

How can to go back to previous view programmatically without UINavigationController?

From Dev

How can to go back to previous view programmatically without UINavigationController?

From Dev

Programmatically go back to the previous fragment

From Dev

How to have a back button always go to the previous view controller

From Dev

How to have a back button always go to the previous view controller

From Dev

Android Webview make back button go to previous page

From Dev

iOS: Go back to previous view

From Dev

android go back to previous view

From Java

Programmatically go back to previous ViewController in Swift

From Dev

angularjs: how to go back to previous page from a nested view when clicking back button of browser?

From Dev

How to go back to previous view controller?

From Dev

How to reload the previous view controller on back button?

From Dev

How to reload the previous view controller on back button?

From Dev

Navigation drawer, handling the back button to go to previous fragments?

From Dev

R Shiny: Go to the previous conditionalPanel using back action button

From Dev

Go to previous route programmatically

From Dev

How to programmatically go back to root view controller with a global function?

From Dev

Go back to previous view controller doesn't work

From Dev

How to go back to previous view controller of different storyboard?

From Dev

Go back to previous view controller doesn't work

From Dev

Click to enlarge image, then click again to make it go back to previous size

From Dev

Detect if back button is pressed AND wait user action to pop to previous view

From Dev

Xamarin MvvmCross Android Prevent Back Button returning to previous View

From Dev

Detect if back button is pressed AND wait user action to pop to previous view

From Dev

Browsing online folder structure - handling back button (save previous view?)

From Dev

Remove previous view controller text from back button

From Dev

Go back to the previous state

From Dev

SWRevealViewController won't work after I go to the child of view controller and go back to the previous view controller

Related Related

  1. 1

    Make back button go to the previous view programmatically

  2. 2

    How can to go back to previous view programmatically without UINavigationController?

  3. 3

    How can to go back to previous view programmatically without UINavigationController?

  4. 4

    Programmatically go back to the previous fragment

  5. 5

    How to have a back button always go to the previous view controller

  6. 6

    How to have a back button always go to the previous view controller

  7. 7

    Android Webview make back button go to previous page

  8. 8

    iOS: Go back to previous view

  9. 9

    android go back to previous view

  10. 10

    Programmatically go back to previous ViewController in Swift

  11. 11

    angularjs: how to go back to previous page from a nested view when clicking back button of browser?

  12. 12

    How to go back to previous view controller?

  13. 13

    How to reload the previous view controller on back button?

  14. 14

    How to reload the previous view controller on back button?

  15. 15

    Navigation drawer, handling the back button to go to previous fragments?

  16. 16

    R Shiny: Go to the previous conditionalPanel using back action button

  17. 17

    Go to previous route programmatically

  18. 18

    How to programmatically go back to root view controller with a global function?

  19. 19

    Go back to previous view controller doesn't work

  20. 20

    How to go back to previous view controller of different storyboard?

  21. 21

    Go back to previous view controller doesn't work

  22. 22

    Click to enlarge image, then click again to make it go back to previous size

  23. 23

    Detect if back button is pressed AND wait user action to pop to previous view

  24. 24

    Xamarin MvvmCross Android Prevent Back Button returning to previous View

  25. 25

    Detect if back button is pressed AND wait user action to pop to previous view

  26. 26

    Browsing online folder structure - handling back button (save previous view?)

  27. 27

    Remove previous view controller text from back button

  28. 28

    Go back to the previous state

  29. 29

    SWRevealViewController won't work after I go to the child of view controller and go back to the previous view controller

HotTag

Archive