3

高级通知会议 708 WWDC 2016结束时。他们谈到了同时进行文本输入和操作。

从 WWDC 会话 708 的 24 分钟开始。

您对派对邀请发表评论并同时接受或拒绝邀请。我试图这样做,但非常不成功。

class NotificationViewController: UIViewController, UNNotificationContentExtension {

var textField = UITextField(frame: CGRect(x: 0, y: 0, width: 100, height: 10))

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any required interface initialization here.
}

override var canBecomeFirstResponder: Bool {

    return true
}

override var inputAccessoryView: UIView? {

    let customView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))

    customView.backgroundColor = UIColor.red

    textField.placeholder = "Type Comment here"

    textField.textColor = UIColor.black

    customView.addSubview(textField)

    print(customView)

    return customView
}

func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {

       if response.actionIdentifier == "comment" {

        becomeFirstResponder()

        textField.becomeFirstResponder()

        print("Its working")

        if let textResponse = response as? UNTextInputNotificationResponse {

            print(textResponse.userText)

            completion(UNNotificationContentExtensionResponseOption.doNotDismiss)
        }

        completion(UNNotificationContentExtensionResponseOption.doNotDismiss)
    }
}

}

4

1 回答 1

2

不知道你的问题到底是什么,但我最近面临一个类似的任务 - 我已经成功地实施了你提到的 WWDC 演讲中的解决方案。
我的问题是我不知道在使用 custom 时如何关闭通知inputAccessoryView。我的解决方案是保存completionHandler,然后在单击自定义中的特定按钮时调用它inputAccessoryView

第一的; 创建函数变量来存储completionHandler:

var savedCompletionBlock:(UNNotificationContentExtensionResponseOption) -> Void = { (UNNotificationContentExtensionResponseOption) in }

然后; 在func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void)保存完成块:

savedCompletionBlock = completion

最后;在您需要的任何地方调用它(例如单击按钮):

func confirmPressed() {
   savedCompletionBlock(.dismiss)
}

如果这对您没有帮助,请告诉我:)

于 2017-04-25T15:07:24.610 回答