【问题标题】:Complication on Apple Watch not showing my informationApple Watch 上的并发症不显示我的信息
【发布时间】:2017-09-06 15:23:36
【问题描述】:

我在 Apple Watch 上遇到了复杂问题。

我正在尝试在复杂功能上显示图像和一些文本。我可以在时钟界面选择并发症,但它没有显示应用程序标题和两行充满“-”字符。

并发症应该显示我的信息,但我看不出我的代码有什么问题

代码如下:

func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
    // This method will be called once per supported complication, and the results will be cached
    handler(nil)
    var template: CLKComplicationTemplateModularLargeColumns?
    switch complication.family {
    case .modularSmall:
        template = nil
    case .modularLarge:
        let modularLargeTemplate =
            CLKComplicationTemplateModularLargeColumns()
        modularLargeTemplate.row1ImageProvider =
            CLKImageProvider(onePieceImage: UIImage(named: "GreenUser")!)
        modularLargeTemplate.row2ImageProvider =
            CLKImageProvider(onePieceImage: UIImage(named: "GreenUser")!)
        modularLargeTemplate.row3ImageProvider =
            CLKImageProvider(onePieceImage: UIImage(named: "GreenUser")!)

        modularLargeTemplate.row1Column1TextProvider = CLKSimpleTextProvider(text: "User: ")
        modularLargeTemplate.row1Column2TextProvider = CLKSimpleTextProvider(text: "ok")

        modularLargeTemplate.row2Column1TextProvider = CLKSimpleTextProvider(text: "Car: ")
        modularLargeTemplate.row2Column2TextProvider = CLKSimpleTextProvider(text: "ok")

        modularLargeTemplate.row3Column1TextProvider = CLKSimpleTextProvider(text: "Environment: ")
        modularLargeTemplate.row3Column2TextProvider = CLKSimpleTextProvider(text: "ok")

        template = modularLargeTemplate
    case .utilitarianSmall:
        template = nil
    case .utilitarianLarge:
        template = nil
    case .circularSmall:
        template = nil
    default:
        template = nil
    }
    handler(template)

}

如果我在代码中间放置一个断点,调试器就会触发它,所以它会执行这段代码。然而,没有像我想要的那样显示。

你能找出错误/遗漏的地方吗?

【问题讨论】:

  • 为什么一开始就叫handler(nil)???您的整个函数没有执行,它立即返回一个 nil 闭包。
  • @DávidPásztor 我试图删除该行,但结果仍然相同
  • 你真的检查过你的函数是否被调用,template在被调用的时候是不是nil
  • 是的,我检查了,该函数被调用并且模板不是 nil。

标签: ios swift apple-watch apple-watch-complication


【解决方案1】:

这只是模板,你需要注意getCurrentTimelineEntry,最简单的就是返回与模板相同的条目(见下文)。 正如 cmets 中提到的其他内容,您还需要在代码中删除 handler(nil)

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
        getLocalizableSampleTemplate(for: complication) {template in
            guard let template = template else {
                handler(nil)
                return
            }
            handler(CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template))
        }
    }

【讨论】:

  • 布罗塔,你为我节省了大量时间。完全不理解时间线的东西,浪费了 2 天时间试图弄清楚为什么我不能显示一个简单的图像。谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多