-1

I have a VPN app I am building, I also have my own DNS servers.

To specify what DNS I want to use I do it in the file VPNUK1.swift under let dns = "1.1.1.1,8.8.8.8"

I have made a settings page that uses SettingsView.swift

I have made a ViewController and added a UISwitch, I have then used @IBOutlet to link it to the SettingsView.swift

However I do not know how to get the UISwitch to change the let dns = "1.1.1.1,8.8.8.8" in VPNUK1.swift from SettingsView.swift

I would like, when the switch is toggled, have it changed to let dns = "185.136.234.36"

MrBenFTW
  • 23
  • 7
  • 1
    You can start changing your declaration from let to var. You can't change you dns object declaring it as a constant. – Leo Dabus Nov 23 '18 at 22:51
  • Oops my bad, I forgot to make it a var. But how do I change it from a different viewController? – MrBenFTW Nov 23 '18 at 22:53
  • Take a look at https://developer.apple.com/documentation/swift/cocoa_design_patterns/managing_a_shared_resource_using_a_singleton – Leo Dabus Nov 23 '18 at 22:55
  • Or https://stackoverflow.com/questions/26207846/pass-data-through-segue – Leo Dabus Nov 23 '18 at 22:57

2 Answers2

0

An IBOutlet is a noun. It points to a view/control in your view controller.

An IBAction is a verb. It lets you specify code that should be run when a user interacts with a control.

You need to control-drag from your storyboard into the soure for the view controller that contains the switch and create an IBAction for the switch, to be invoked on a value changed event.

In your IBAction check the state of the sender's isOn property.

You then need a way to communicate the change to your other view controller. To help with that you're going to have to explain how the view controllers relate to each other and how the user gets from one to the other. Are they both on the screen at the same time? Does one view controller present the other one modally?

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

In settings view controller use a if / else ui switch to set UserDefaults.standard.set("1.1.1.1", forKey: "DNS")

In the vpn view controller: "dns": UserDefaults.standard.string(forKey: "DNS")!

MrBenFTW
  • 23
  • 7