0

I have some variables in ViewController.swift file which is located in root of the application (default location) and I want to use them in KeyboardViewController.swift file which is located in custom keyboard target

how can I access those variables?

for being more clear I want to access the variables I have declared in ViewController.swift from a view controller in a target.

the photo will help too

you can see the sample code I have problem with here:

ViewController.swift

import UIKit

public var globalVar = "defaultColor"

class ViewController: UIViewController {

    @IBAction func buttonA(sender: UIButton) {

        globalVar = "ColorA"

    }

    @IBAction func buttonB(sender: UIButton) {

        globalVar = "ColorB"

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

KeyboardViewController.swift

import UIKit

class KeyboardViewController: UIInputViewController {

    override func updateViewConstraints() {
        super.updateViewConstraints()

        // Add custom view sizing constraints here
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if (globalVar == "defaultColor" /* The error is here it says use of unresolverd identifier 'globalVar' */) {

        } else if (globalVar == "ColorA") {

        } else if (globalVar == "ColorB") {

        }

    }

}
Ali Lashini
  • 427
  • 1
  • 5
  • 17
  • Could you include your code instead of a screen shot? – kye Mar 26 '16 at 23:47
  • 1
    @AliLashini Because if we want to try your code, we can't just copy/paste it from your question, we have to retype everything looking at your screenshot. – Valentin Mar 27 '16 at 10:07

2 Answers2

0

If you need access to a file from within an extension, all you need to do is add the file to your extension target.

However if you are dealing with extensions, be aware that you cannot access the same instances of the class that exists in each. You will have to create a separate instance inside the extension, and use that instance.

GetSwifty
  • 7,568
  • 1
  • 29
  • 46
  • I want the user choose his own effect in application ViewController, and then I set the code for that effect which user choosed in KeyboardViewController what's the best way for doing this? – Ali Lashini Mar 27 '16 at 09:59
  • I would recommend googling it :) here's a similar question to get your started: http://stackoverflow.com/questions/24015506/communicating-and-persisting-data-between-apps-with-app-groups?lq=1 – GetSwifty Mar 28 '16 at 05:34
0

My problem solved with app group. Thanks Everybody

the solution:

1: Switching on App Groups for both your application and Target by clicking on application > Capabilities

2: Clicking on plus sign and set a name for app group (ex: group.company.appname) note that for both application and target should be same

3: setting NSUserDefaults like this

var defaults = NSUserDefaults(suiteName: "group.company.appname")
defaults?.setObject("Worked!", forKey: "Alarm")
defaults?.synchronize()

4: Reading NSUserDefaults where you want (like in a target) like this

// Check for null pressEffect
if let restoredDefault =  defaults!.stringForKey("Alarm") {
     print(restoredDefault)
} else {
     print("Not Working!")
}
Ali Lashini
  • 427
  • 1
  • 5
  • 17