(Swift) How to use UISwitch to change var
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"
ios swift var uiswitch
add a comment |
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"
ios swift var uiswitch
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 developer.apple.com/documentation/swift/cocoa_design_patterns/…
– Leo Dabus
Nov 23 '18 at 22:55
Or stackoverflow.com/questions/26207846/pass-data-through-segue
– Leo Dabus
Nov 23 '18 at 22:57
add a comment |
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"
ios swift var uiswitch
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"
ios swift var uiswitch
ios swift var uiswitch
asked Nov 23 '18 at 22:48
MrBenFTWMrBenFTW
12
12
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 developer.apple.com/documentation/swift/cocoa_design_patterns/…
– Leo Dabus
Nov 23 '18 at 22:55
Or stackoverflow.com/questions/26207846/pass-data-through-segue
– Leo Dabus
Nov 23 '18 at 22:57
add a comment |
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 developer.apple.com/documentation/swift/cocoa_design_patterns/…
– Leo Dabus
Nov 23 '18 at 22:55
Or stackoverflow.com/questions/26207846/pass-data-through-segue
– Leo Dabus
Nov 23 '18 at 22:57
1
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
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
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 developer.apple.com/documentation/swift/cocoa_design_patterns/…
– Leo Dabus
Nov 23 '18 at 22:55
Take a look at developer.apple.com/documentation/swift/cocoa_design_patterns/…
– Leo Dabus
Nov 23 '18 at 22:55
Or stackoverflow.com/questions/26207846/pass-data-through-segue
– Leo Dabus
Nov 23 '18 at 22:57
Or stackoverflow.com/questions/26207846/pass-data-through-segue
– Leo Dabus
Nov 23 '18 at 22:57
add a comment |
2 Answers
2
active
oldest
votes
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?
The view controllers use a 'PageViewController'
– MrBenFTW
Nov 25 '18 at 22:18
I have done it properly with user defaults now
– MrBenFTW
Nov 25 '18 at 23:46
add a comment |
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")!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453638%2fswift-how-to-use-uiswitch-to-change-var%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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?
The view controllers use a 'PageViewController'
– MrBenFTW
Nov 25 '18 at 22:18
I have done it properly with user defaults now
– MrBenFTW
Nov 25 '18 at 23:46
add a comment |
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?
The view controllers use a 'PageViewController'
– MrBenFTW
Nov 25 '18 at 22:18
I have done it properly with user defaults now
– MrBenFTW
Nov 25 '18 at 23:46
add a comment |
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?
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?
answered Nov 23 '18 at 23:39
Duncan CDuncan C
92.3k13114196
92.3k13114196
The view controllers use a 'PageViewController'
– MrBenFTW
Nov 25 '18 at 22:18
I have done it properly with user defaults now
– MrBenFTW
Nov 25 '18 at 23:46
add a comment |
The view controllers use a 'PageViewController'
– MrBenFTW
Nov 25 '18 at 22:18
I have done it properly with user defaults now
– MrBenFTW
Nov 25 '18 at 23:46
The view controllers use a 'PageViewController'
– MrBenFTW
Nov 25 '18 at 22:18
The view controllers use a 'PageViewController'
– MrBenFTW
Nov 25 '18 at 22:18
I have done it properly with user defaults now
– MrBenFTW
Nov 25 '18 at 23:46
I have done it properly with user defaults now
– MrBenFTW
Nov 25 '18 at 23:46
add a comment |
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")!
add a comment |
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")!
add a comment |
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")!
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")!
answered Nov 25 '18 at 23:49
MrBenFTWMrBenFTW
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453638%2fswift-how-to-use-uiswitch-to-change-var%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 developer.apple.com/documentation/swift/cocoa_design_patterns/…
– Leo Dabus
Nov 23 '18 at 22:55
Or stackoverflow.com/questions/26207846/pass-data-through-segue
– Leo Dabus
Nov 23 '18 at 22:57