0

I'm trying to create a settings view controller in my container app for my customKeyboard for iOS8 in Swift. I have create a Bool() and I got right what I need by that, but now i need to get the Keyboard target checking the bool value and I can't import the other swift file..

I've this code:

ViewControllerSettings.swift:

@IBOutlet weak var Switch1: UISwitch!

var dotOn = Bool()


override func viewDidLoad() {
    super.viewDidLoad()


    if NSUserDefaults.standardUserDefaults().boolForKey("dotOn") == true {

        Switch1.setOn(true, animated: true)

    } else {

        Switch1.setOn(false, animated: true)

    }


  @IBAction func backSave(sender: UIButton) {

    if Switch1.on == true {


        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "dotOn")

    } else {

        NSUserDefaults.standardUserDefaults().setBool(false, forKey: "dotOn")

    }
  }

And it works great! But in my KeyboardViewController.swift in another target of the App I have in the viewDidAppear(animated: true) :

  if NSUserDefaults.standardUserDefaults().boolForKey("dotOn") == true {

let bSelector : Selector = "dotAfterSpace:"
    let doubleTapGesture = UITapGestureRecognizer(target: self, action:   bSelector)
    doubleTapGesture.numberOfTapsRequired = 2
    space.addGestureRecognizer(doubleTapGesture)

 } else { }

And this function without the "if" works well... what should I do?


After some advice I wrote this code... but where am I wrong?

In KeyboardViewController.swift:

class KeyboardViewController: UIInputViewController {
     var otherController = ViewControllerSettings()

override func viewDidAppear(animated:Bool) {
   super.viewDidAppear(animated)

   KeyboardViewController().otherController = ViewControllerSettings()

    if otherController.dotOn {

        let bSelector : Selector = "dotAfterSpace:"
        let doubleTapGesture = UITapGestureRecognizer(target: self, action: bSelector)
        doubleTapGesture.numberOfTapsRequired = 2
        space.addGestureRecognizer(doubleTapGesture)

    } else { }

    let aSelector : Selector = "spacePressed:"
    let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)
    tapGesture.numberOfTapsRequired = 1
    space.addGestureRecognizer(tapGesture)

}

In ViewControllerSettings.swift:

class ViewControllerSettings: UIViewController {

 var dotOn = Bool()
Francesco
  • 169
  • 1
  • 1
  • 14

1 Answers1

0

Create a property on both sides of your controllers:

class ViewController1:<CtrlType1> {
   var otherController:<CtrlType2>

   func doSomething() {
     if otherController.sharedBool { .... }
   }
 }

class ViewController2:<CtrlType2> {
   var sharedBool = false

 }

In the place where you create both instances (or where they have been created by IB) add

 viewController1.otherController = viewController2

This way you can use the bool in both instances.

Another approach would be to use a singleton, but for a simple bool that would be overkill.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • with you mean the type of the two controller? I have a UIViewController and a UIInputViewController... It's ok? Because it doesn't work.. – Francesco Jan 27 '15 at 13:38
  • The types in angle brackets must be replaced by real ones you use. In OSX it would be NSViewController. – qwerty_so Jan 27 '15 at 13:40
  • But it says me that < and > are not prefix unary operators and doesn't read the property.. – Francesco Jan 27 '15 at 13:49
  • And if I put instead of the name of my UIVIewController: var otherController = ViewControllerSettings() it doesn't do nothing when running... – Francesco Jan 27 '15 at 13:52
  • Of course not. You must assign it from outside. This way you create a new instance. – qwerty_so Jan 27 '15 at 14:32
  • As I said: `var otherController = ViewControllerSettings()` creates a new instance which is not your other view controller. You need to assign it in ViewControllerSettings or from the controlling class. – qwerty_so Jan 27 '15 at 16:06