Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

Call delegate when user tap "Back" button

I need a way to change text depending of user input, when user tap back button. I followed that solution: Find out if user pressed the back button in uinavigationcontroller?

and did add following code in viewDidLoad:

if ([self isMovingFromParentViewController]) {
    NSLog(@"isMoved");
    [self.delegate stringChangedTo:self.myTextField.text atIndex:self.indexToPass];
}

However, nothing changed. More to say, method is not called (NSLog dont output a string).

How could i find a way to call delegate when user tap back button?

Upvotes: 0

Views: 90

Answers (2)

Nicolas Miari
Nicolas Miari

Reputation: 16246

There is also a UINavigationControllerDelegate protocol. You can get notified when a given view controller is shown by implementing either of these:

-navigationController:willShowViewController:animated:

-navigationController:didShowViewController:animated:

ADDENDUM: In my opinion, using the delegate is a cleaner design, because you get notified precisely when a navigation event occurs. View controller life cycle methods such as -viewDidDisappear:, etc. can get called when you present/dismiss a modal view controller, and require that you add logic to discern those.

Upvotes: 1

rmaddy
rmaddy

Reputation: 318804

That code needs to be in viewWillDisappear: or viewDidDisappear:. not viewDidLoad.

viewDidLoad is called when the view controller's view is loaded. You want to call the delegate when the view controller is being dismissed.

Upvotes: 2

Related Questions