Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching for a view controller of the matching type is useful outside of segues #6

Closed
sharplet opened this issue Aug 30, 2016 · 1 comment

Comments

@sharplet
Copy link
Contributor

It would be nice to expose the functionality of UIStoryboardSegue.destinationViewController(ofType:) in a more general way that is useful in different contexts.

For example, lets say you wanted to configure the tabs in a UITabBarController:

final class MyTabBarController: UITabBarController {
  override func viewDidLoad() {
    super.viewDidLoad()

    nextTab: for tab in viewControllers {
      for child in tab.hierarchy {
        switch child {
        case let feed as FeedViewController:
          // configure feed
          continue nextTab
        case let profile as ProfileViewController:
          // configure profile
          continue nextTab
        }
      } 
    }
  }
}

For each tab, this code searches the view controller hierarchy until it finds a matching view controller, then continues to the next tab.

This would be more concise:

for tab in viewControllers {
  if let feed = tab.childViewController(ofType: FeedViewController.self) {
    // configure feed
  }

  if let profile = tab.childViewController(ofType: ProfileViewController) {
    // configure profile
  }
}

But has the disadvantage of searching each tab N times, where N is the number of different things you need to configure.

I think there should be a way to encapsulate the first search strategy using a higher-level construct.

@sharplet
Copy link
Contributor Author

I think something like this could work:

func viewDidLoad() {
  super.viewDidLoad()

  controller.configureTabs { child in
    switch child {
    case let feed as Feed:
      // configure feed
      return .configured

    case let profile as Profile:
      // configure profile
      return .configured

    default:
      return .ignored
    }

    // or...

    if let feed = child as? Feed {
      // configure feed
      return .configured
    }

    if let profile = child as? Profile {
      // configure profile
      return .configured
    }

    return .ignored
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant