(Swift) How to use UISwitch to change var












-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"










share|improve this question


















  • 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














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"










share|improve this question


















  • 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








-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"










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












2 Answers
2






active

oldest

votes


















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?






share|improve this answer





















  • 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



















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")!






share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    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?






    share|improve this answer





















    • 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
















    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?






    share|improve this answer





















    • 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














    0












    0








    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?






    share|improve this answer












    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?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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













    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")!






    share|improve this answer


























      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")!






      share|improve this answer
























        0












        0








        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")!






        share|improve this answer












        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")!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 23:49









        MrBenFTWMrBenFTW

        12




        12






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)