如何快速调用TableViewCell内部的对象?

时间:2019-07-11 03:49:47

标签: ios swift uitableview onesignal

我正在尝试为我的应用使用OneSignal IOS Native SDK。我在课程中加入了OSPermissionObserver and OSSubscriptionObserver。它需要添加协议存根,并且在协议内部,我需要在代码中调用allowNotificationSwitch。但是我不知道该怎么办。

这就是我的TableViewCell的样子。 MenuCell中的indexPath.row = 0Switch个对象。由于indexPath.row 0仅具有switch对象,因此我以编程方式完成了此操作。我使用下面的代码在cellRowAt内创建开关并创建了switchChanged函数。问题是如何在OSPermissionObserver和OSSubscriptionObserver协议内调用allowNotificationSwitch?我注释掉了我将称为allowNotificationSwitch的部分。希望我能以您能理解的方式进行解释。希望能为您提供帮助。非常感谢。

AllowNotificationSwitch

 if indexPath.row == 0 {
            let allowNotificationSwitch = UISwitch(frame: .zero)
            allowNotificationSwitch.setOn(false, animated: true)
            allowNotificationSwitch.tag = indexPath.row
            allowNotificationSwitch.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
            cell.accessoryView = allowNotificationSwitch
        }

 @objc func switchChanged(_ sender : UISwitch!) {

    let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
    let isSubscribed = status.subscriptionStatus.subscribed

    if isSubscribed == true {
        sender.isOn = true
        sender.isUserInteractionEnabled = true
    }

    OneSignal.add(self as OSPermissionObserver)
    OneSignal.add(self as OSSubscriptionObserver)

    if !sender.isOn {
        OneSignal.setSubscription(false)
    } else {
        OneSignal.setSubscription(true)
    }

    print("table row switch change \(sender.tag)")
    print("the switch is \(sender.isOn ? "ON" : "OFF")")
}

OSPermissionObserver和OSSubscriptionObserver协议(基于OneSignal文档)

 func onOSPermissionChanged(_ stateChanges: OSPermissionStateChanges!) {
    if stateChanges.from.status == OSNotificationPermission.notDetermined {
        if stateChanges.to.status == OSNotificationPermission.authorized {
           // allowNotificationsSwitch.isUserInteractionEnabled = true
        } else if stateChanges.to.status == OSNotificationPermission.denied {
            displaySettingsNotification()
        }
    } else if stateChanges.to.status == OSNotificationPermission.denied { // DENIED = NOT SUBSCRIBED
      //  allowNotificationsSwitch.isUserInteractionEnabled = false
    }
}

func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
    if stateChanges.from.subscribed && !stateChanges.to.subscribed { // NOT SUBSCRIBED != DENIED
       // allowNotificationsSwitch.isOn = false
    } else if !stateChanges.from.subscribed && stateChanges.to.subscribed {
       // allowNotificationsSwitch.isOn = true
       // allowNotificationsSwitch.isUserInteractionEnabled = true
    }
}

0 个答案:

没有答案