VansFannel
VansFannel

Reputation: 45921

Do something when user taps on UINavigationController back button

I'm developing an iOS application with latest SDK and XCode.

I have a configuration screen and I need to save settings before user comes back to previous screen. This viewcontroller is called SettingsViewController.

I'm using a Navigation Controller on a Storyboard and I need to detect when user taps on Back button.

I could add this code on - (void)viewWillDisappear:(BOOL)animated. What do you think?

How can I detect when user taps on back button on SettingsViewController?

Upvotes: 1

Views: 1140

Answers (2)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

Try this one

//Put this viewDidLoad and make sure you have a back.png that mimics back button
UIImage *buttonImage = [UIImage imageNamed:@"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(backPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;

This is your method

- (void)backPressed:(id)sender {
     //your code to save and also pop your view controller
  }

Upvotes: 1

iArezki
iArezki

Reputation: 1271

you can add your buttonitem with self.navigationItem.leftButtonItem wehn the user click in the button you cal the methode defined in the selector of you button

Upvotes: 1

Related Questions