0

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?

Community
  • 1
  • 1
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • add block varaible to view controller for call back, when the back is pressed from custom back handler, in that method, capture the callback variable form the previoous view controller and store it in an instance variable. The call back completition value can be set in the viewdiddisappear or viewwilldisappear of the viewcontorller who's back button is pressed. – Larry Pickles Nov 19 '15 at 00:34

2 Answers2

2

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
1

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.

Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189