سؤال

I have an iOS app with 3 tabs:

  • Home
  • Tab1
  • Tab2

In "Home", user can navigate to a view - UIViewcontroller1 - to view the details. When the user pressed "Back" in UIViewcontroller1 to go back to "Home", I would like to detect in Home viewDidLoad() that the user came here by pressing "Back" in UIViewcontroller1.

Is there a simple way to detect this?

هل كانت مفيدة؟

المحلول

I know of a couple of ways to do this, none of them is exactly what you want but they might work.

First, you can detect the back on UIViewController1's viewWillDisappear, like this:

if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
    //Do what you want to do here
}

Second, you could make Home UIViewController1's delegate and call whatever method you want.

Third, you could set a variable when Home pushes UIViewController1 onto the stack and check it on viewDidAppear.

نصائح أخرى

A delegation pattern could solve that issue.

You should have a look at this post:

Passing data between View Controllers

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top