ios - Nib 内的 UISwitch 值未更新

标签 ios swift

我的开关位于 nib 内,而 nib 位于 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 header 内的 nib”这样的东西。每次您通过说 loadNibNamed 加载一个 nib 时,您都会得到它包含的 View 的一个新副本。因此,每次您调用 pushNotificationSwitchTapped 时,您都会从 nib 中获得一个全新的全新开关,并将其粘贴到您的界面中。这可能不是您想要的!当然,开关来自只有一种状态的 nib,即您在 nib 编辑器中设置的状态:打开。

你需要完全放弃这种基于对 nib 是什么的误解的错误架构。你应该加载 nib 一次,然后你应该通过与开关本身对话来引用 开关,它现在在你的界面中。

关于ios - Nib 内的 UISwitch 值未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58551643/

相关文章:

iphone - 将 NSString 转换为 NSDate

ios - 我可以向 Apple Store 提交同一个 App 的多个版本吗

ios - 过去曾使用 Facebook 登录过您的应用的用户重新登录

ios - 在 UICollectionView 和 UITableView 中优化预加载/预取图像

ios - Swift 4 - 使用 FBSDKGraphRequest 查询 facebook

ios - 使用 UICollectionViewCell 内的 Textfield 更新标签

swift - 打印用户点击的NSTableView的行号

iOS:在我的通用应用程序中处理文件下载的最佳方式

ios - 如何在 swift 中就地改变输入数组?

ios - SKSpriteNode 替换为 SKTexture