我的开关位于笔尖内,而笔尖位于tableview标头内。当我单击开关时,它只会执行pushSwitch.isOn而不是!pushSwitch.isOn。它只给我pushSwitch内部的值,似乎不在!pushSwitch.isOn内部
谢谢

protocol PhoneNotificationHeaderViewDelegate: class {
    func copyPreferredSettingsRequested()
    func textNotificationSwitchTapped()
    func pushNotificationSwitchTapped()
}

class PhoneNotificationHeaderView: UITableViewHeaderFooterView {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var textSwitch: UISwitch!
    @IBOutlet weak var pushSwitch: UISwitch!
    @IBOutlet weak var copyPreferredSettingsButton: UIButton!
    @IBOutlet weak var notificationView: UIView!

weak var delegate: PhoneNotificationHeaderViewDelegate?

    UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
}

@IBAction func textNotificationSwitchTapped(_ sender: AnyObject) {
    self.delegate?.textNotificationSwitchTapped()
}

@IBAction func pushNotificationSwitchTapped(_ sender: UISwitch) {
    self.delegate?.pushNotificationSwitchTapped()
}

@IBAction func copyPreferredSettingsButtonTapped() {
    self.delegate?.copyPreferredSettingsRequested()
}

override var backgroundColor: UIColor? {
    didSet {
        print("backgroundColor")
    }
}

这是我在VC中的开关:
extension PhoneNotificationViewController: PhoneNotificationHeaderViewDelegate {
func pushNotificationSwitchTapped() {

    guard let phoneNotificationHeader = Bundle(for: type(of: self)).loadNibNamed("PhoneNotificationHeaderView", owner: self, options: nil)?.first as? PhoneNotificationHeaderView else {
        return
    }

    if phoneNotificationHeader.pushSwitch.isOn{
        //Disable Firebase from sending
        Globals.sharedInstance.pushStatus = true
        phoneNotificationHeader.pushSwitch.setOn(false, animated: true)
    }else{
        Globals.sharedInstance.pushStatus = false
        phoneNotificationHeader.pushSwitch.setOn(true, animated: true)
    }
    self.refreshUI()

}

最佳答案

没有“tableview标头内部的笔尖”之类的东西。每次通过说loadNibNamed加载笔尖时,都会得到它包含的视图的新副本。因此,每次调用pushNotificationSwitchTapped时,您都会从笔尖获得一个全新的开关,并将其粘贴在界面中。那可能不是您想要的!当然,开关仅在一种状态下来自笔尖,即您在笔尖编辑器中设置的状态:打开。

您需要基于对笔尖的误解而完全放弃这种错误的体系结构。您应该加载一次笔尖,然后再通过与交换机本身进行通信来引用该交换机,该交换机现在位于您的界面中。

09-25 16:43