使用变量而不是交互式通知字符串



我试图创建通知与多个选项供用户选择。我把它们修好了。

但是,我不想使用硬编码字符串作为选项按钮的标题。相反,我想使用一个变量(这是一个字符串),但它不起作用!

let word1Action = UIMutableUserNotificationAction()
    word1Action.identifier = "word"
    word1Action.title = "word"
    word1Action.activationMode = UIUserNotificationActivationMode.Background
    word1Action.destructive = false
    word1Action.authenticationRequired = false

在上面的代码中,"word"显示在通知按钮中。

let word2Action = UIMutableUserNotificationAction()
    word2Action.identifier = "word_1"
    word2Action.title = "0(word_1)"
    word2Action.activationMode = UIUserNotificationActivationMode.Background
    word2Action.destructive = false
    word2Action.authenticationRequired = false

在上面的代码中,通知按钮上只显示"0"。

let word3Action = UIMutableUserNotificationAction()
    word3Action.identifier = "word_2"
    word3Action.title = word_2
    word3Action.activationMode = UIUserNotificationActivationMode.Background
    word3Action.destructive = false
    word3Action.authenticationRequired = false

在上面的代码中,通知按钮为空。

那么为什么只有一个基本字符串可以工作呢?

我已经验证了我使用的变量实际上是一个字符串,我没有得到其他错误,只是空白通知。

任何想法吗?这是iOS的限制吗?

谢谢!

好吧,现在我觉得自己很傻。我把上面的代码贴在AppDelegate

didFinishLaunchingWithOptions

由于我试图访问的变量是全局的,还没有初始化,所以它们显示为空白。

我想我今天还需要一杯咖啡。: -)

最新更新