1

Apologies, if this is a little unclear - I'm a noob when it comes to iOS programming. Here's the scenario:

I've got a LogInView, a CategoryView, a CheckerView, a WalkthroughView, and a LandingPageView.

The user starts at LogInView, and depending on the app's bluetooth state, and whether or not the user has been registered, either goes to:

  • CheckerView (Registered, Bluetooth Off)
  • LandingPageView (Registered, Bluetooth On)
  • CategoryView (Unregistered)

If the user hits CategoryView, depending on the state of his bluetooth connection, he goes to either (this part, so far, works okay):

  • WalkthroughView (Bluetooth on)
  • CheckerView (Bluetooth off)

The catch is that CategoryView will always go through WalkthroughView, regardless of whether or not bluetooth is on. So, here's what my storyboard looks like:

A right hot mess

A right hot mess, I know. Since both LogInView and CategoryView can, at some point, go into CheckerView, I need a way to check which of the segues was used, such that:

  • CheckerView will always go into LandingView if the previous view was LogInView, and
  • It will always go into WalkthroughView if the previous view was CategoryView.

I'm vaguely aware of a prepareForSegue function, but I've no idea yet how to use it, nor where to put it (from the previous page, or on the receiving page?)

Any suggestions? Thanks.

oyvindhauge
  • 3,496
  • 2
  • 29
  • 45
zack_falcon
  • 4,186
  • 20
  • 62
  • 108
  • Have you read the documentation for prepareForSegue in the UIViewController class reference? Have you looked at any tutorials on using segues? You can easily set a Boolean on Checker View if it is coming from login view using prepareForSegue. – Paulw11 Aug 03 '15 at 12:50
  • Another thought would be to test registered/unregistered and based on that outcome, use `performSegueWithIdentifier` to determine which segue you'll use. Then test Bluetooth enabled/disabled. In `prepareForSegue`, you'd set what you need to set on the next VC. http://stackoverflow.com/questions/7864371/how-to-pass-prepareforsegue-an-object – Adrian Aug 03 '15 at 13:00

2 Answers2

2

It sounds like you're testing conditions to determine where you'll segue. If that's the case, perhaps you could test conditions (registered/unregistered, Bluetooth enabled/disabled). Based on the various conditions, you could use performSegueWithIdentifier to determine where to go next and set up the next ViewController in prepareForSegue using the a segue identifier, rather than "looking back" to see where you came from.

Adrian
  • 16,233
  • 18
  • 112
  • 180
1

My standard suggestion is that once application state becomes complex it should be moved out of view controllers and into an actual Data Model object.

The Data Model can either be a custom class you create (preferred for scalability). Or, in this case where there's not a great volume of information being shared, you could look at putting it into NSUserDefaults and reading from there when needed.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57