10

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];
Colton Anglin
  • 431
  • 3
  • 9
  • 19
  • And where is your line showing selector? – El Tomato Feb 19 '14 at 02:08
  • UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 64)]; [self.view addSubview:myBar]; UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:nil action:nil]; [leftButton addTarget:self action:@selector(method) – Colton Anglin Feb 19 '14 at 02:10

3 Answers3

16

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];
}
larva
  • 4,687
  • 1
  • 24
  • 44
11

You could write this in your viewDidLoad method:

UIBarButtonItem *btn=[[UIBarButtonItem alloc] initWithTitle:@"Back"     style:UIBarButtonItemStyleBordered target:self action:@selector(btnClick)];
self.navigationItem.leftBarButtonItem=btn;

and then use this:

// call of method
-(void)btnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
Mohit
  • 3,708
  • 2
  • 27
  • 30
3

[self dismissViewControllerAnimated:YES completion:nil];

Sebastián Lara
  • 5,355
  • 2
  • 28
  • 21