我正在尝试在我的容器应用中为Swift中的iOS8的customKeyboard创建一个设置视图控制器。我已经创建了一个Bool(),我得到了我需要的东西,但现在我需要让键盘目标检查bool值,我无法导入其他swift文件..
我有这段代码:
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")
}
}
它很棒!但是在我的 KeyboardViewController.swift 中我在viewDidAppear(animated:true)中的另一个App目标中:
if NSUserDefaults.standardUserDefaults().boolForKey("dotOn") == true {
let bSelector : Selector = "dotAfterSpace:"
let doubleTapGesture = UITapGestureRecognizer(target: self, action: bSelector)
doubleTapGesture.numberOfTapsRequired = 2
space.addGestureRecognizer(doubleTapGesture)
} else { }
没有“if”的这个功能效果很好......我该怎么办?
经过一些建议我写了这段代码......但我错在哪里?
在 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)
}
在 ViewControllerSettings.swift :
中class ViewControllerSettings: UIViewController {
var dotOn = Bool()
答案 0 :(得分:0)
在控制器的两侧创建一个属性:
class ViewController1:<CtrlType1> {
var otherController:<CtrlType2>
func doSomething() {
if otherController.sharedBool { .... }
}
}
class ViewController2:<CtrlType2> {
var sharedBool = false
}
在您创建两个实例的位置(或IB创建的位置)添加
viewController1.otherController = viewController2
这样你可以在两个实例中使用bool。
另一种方法是使用单身人士,但对于一个过度杀伤的简单布尔来说。