从Swift 4中的Popover Viewcontroller实时更新UIViewController

时间:2018-03-30 14:39:59

标签: swift debugging uiviewcontroller scenekit switchcontrol

现在我正在尝试使用SceneKit DebugOptions。 我正在尝试使用Popover ViewController中的开关控制器实时更新/显示Scenekits调试选项。

我已经尝试过很多东西,例如UserDefaults,Delegation和Protocols,但是我不能实时看到结果,每次我必须杀死应用程序重新启动它才能看到结果

所以,如果有人能回答我的问题,我会很高兴:D

enter image description here

扩展我添加到我的MainVC

extension ViewController: UIPopoverPresentationControllerDelegate, DebugOptions {

func wireFrameEnabled(enabled: Bool) {
    if enabled == true {
        print(enabled)
    }
}

func showCameraEnabled(enabled: Bool) {

}

func showAllDebugOptions(enabled: Bool) {

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard let popoverController = segue.destination.popoverPresentationController, let button = sender as? UIButton else { return }
    popoverController.delegate = self
    popoverController.sourceRect = button.bounds
    let debugMenuVC = popoverController.presentedViewController as! DebugMenuVC
    debugMenuVC.delegate? = self
}

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

}

协定

protocol DebugOptions {
func wireFrameEnabled(enabled: Bool)
func showCameraEnabled(enabled: Bool)
func showAllDebugOptions(enabled: Bool)

}

DebugMenuVC

class DebugMenuVC: UIViewController {
@IBOutlet weak var bgView: UIView!
@IBOutlet weak var showWireFrameSwitch: UISwitch!
@IBOutlet weak var showCameraSwitch: UISwitch!
@IBOutlet weak var showAllSwitch: UISwitch!

var delegate: DebugOptions?

override func viewWillLayoutSubviews() {
    preferredContentSize = CGSize(width: 150, height: 300)
}

override func viewDidLoad() {
    super.viewDidLoad()
    buttonCheck()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)


}

@IBAction func aSwitchBtnWasPressed( _ sender: UISwitch ) {
    if (sender.tag == 0) && (sender.isOn == true) {
        userDefaults.set(true, forKey: SHOW_WIRE_FRAME)
        delegate?.wireFrameEnabled(enabled: true)


    } else if (sender.tag == 0) && (sender.isOn == false) {
        userDefaults.set(false, forKey: SHOW_WIRE_FRAME)
        delegate?.wireFrameEnabled(enabled: false)

    }

}

func buttonCheck() {
    if userDefaults.bool(forKey: SHOW_WIRE_FRAME) == true{
        showWireFrameSwitch.isOn = true
    } else {
        showWireFrameSwitch.isOn = false
    }

}

}

1 个答案:

答案 0 :(得分:0)

debubMenuVC.delegate中的

不应该是可选的。这就是委托方法总是失败的原因:D

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard let popoverController = segue.destination.popoverPresentationController, let button = sender as? UIButton else { return }
    popoverController.delegate = self
    popoverController.sourceRect = button.bounds
    let debugMenuVC = popoverController.presentedViewController as! DebugMenuVC
    debugMenuVC.delegate? = self
}