1

I don't know how to make the title specific, I'll explain what I need here.

My question:

This is for a weather app, where I allow the user to change the city they want to check the weather for. The problem is that I created the view controllers programmatically. Meaning the prepareForSegue override function doesn't apply.

Now I did find the answer for what to do instead of the prepareForSegue (bellow is the code), however, where should I be calling the function since it is not an override.

Code:

func prepareForSegue() {
     let changeCityVC = ChangeCityViewController()
     changeCityVC.delegate = self
     present(changeCityVC, animated: true, completion: nil)
}

Thanks,

pacification
  • 5,838
  • 4
  • 29
  • 51
The Don
  • 63
  • 1
  • 7
  • This [answer](https://stackoverflow.com/questions/43797334/perform-a-segue-programmatically) answers a similar question. – Andreas Pardeike Sep 05 '18 at 18:20
  • No it doesn’t, the one you are referring to assumes the view controllers were created in the storyboard. I clearly stated that my view controllers were created programmatically. – The Don Sep 05 '18 at 18:45
  • The answer clearly said “if you don’t have a storyboard”. But it looks like you got your answer anyway. – Andreas Pardeike Sep 05 '18 at 18:49

2 Answers2

0

Two options jump into my head:

  1. Add a property to the view controller that is going to be presented, something like "titleText". Then when you create that view controller, set the value for that property and use viewDidLoad to assign titleText to the title of the view controller that you are transitioning too.

  2. Create a protocol and add a delegate property on the new view controller of that type. In the first view controller, assign the creator as the delegate. Then in viewDidLoad (or wherever), the new view controller can ask the delegate (the presenter in the case) for whatever info it needs (i.e. the title).

ghostatron
  • 2,620
  • 23
  • 27
0

Your prepareForSegue method doesn't actually prepare for a segue. It simply creates a VC and presents it. So it should really be called presentChangeCityVC or something like that.

As to where to call the method, just call it whenever you want to present your VC! Let's suppose you have a button that says "Change City". You can call the method in the button's target:

 func changeCityButtonPressed() {
    presentChangeCityVC()
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313