简体   繁体   English

为什么我的 Apple Watch 并发症没有显示任何东西?

[英]Why doesn't my Apple Watch complication show anything?

I created an app using Xcode's "iOS App with Watchkit App" template, went into TARGETS and checked Complications Configuration > Supported Families > Graphic Corner .我使用 Xcode 的“带有 Watchkit 应用程序的 iOS 应用程序”模板创建了一个应用程序,进入TARGETS并检查了Complications Configuration > Supported Families > Graphic Corner I opened ComplicationController.swift in the Extension and modified getCurrentTimelineEntry() :我在扩展中打开了ComplicationController.swift并修改了getCurrentTimelineEntry()

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
    let cornerTemplate = CLKComplicationTemplateGraphicCornerStackText()
    cornerTemplate.outerTextProvider = CLKSimpleTextProvider(text: "Outer")
    cornerTemplate.innerTextProvider = CLKSimpleTextProvider(text: "Inner")
    let entry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: cornerTemplate)
    handler(entry)
}

I also modified getLocalizableSampleTemplate() to provide a sample, and this is not working either:我还修改了 getLocalizableSampleTemplate() 以提供示例,但这也不起作用:

func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
    let cornerTemplate = CLKComplicationTemplateGraphicCornerStackText()
    cornerTemplate.outerTextProvider = CLKSimpleTextProvider(text: "Outer")
    cornerTemplate.innerTextProvider = CLKSimpleTextProvider(text: "Inner")
    handler(cornerTemplate)
}

When I run the app in the simulator or on my phone/watch and select the complication as one of the graphic corners, I expect to see "Outer" and "Inner".当我在模拟器或我的手机/手表上运行该应用程序并且 select 复杂功能作为图形角之一时,我希望看到“外部”和“内部”。 Instead it shows the name of my app for one and "---" for the other.相反,它显示一个应用程序的名称,另一个显示“---”。

What am I doing wrong?我究竟做错了什么?

This is some of my code that is currently working:这是我目前正在运行的一些代码:

var graphicCornerComplication: CLKComplicationTimelineEntry? {

        guard #available(watchOSApplicationExtension 5.0, *) else {
            return nil
        }

        let innerTextProvider = CLKSimpleTextProvider(text: "Inner")
        let outerTextProvider = CLKSimpleTextProvider(text: "Outer")

        let template = CLKComplicationTemplateGraphicCornerStackText()
        template.outerTextProvider = outerTextProvider
        template.innerTextProvider = innerTextProvider

        let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
        return timelineEntry

    }

A few considerations:几点考虑:

  • Have you implemented your getLocalizableSampleTemplate code?您是否实现了getLocalizableSampleTemplate代码? This should be the first thing you do when configuring complications.这应该是您配置复杂功能时要做的第一件事。 You should have something ready to show immediately when users scroll through complication slots and see yours.当用户滚动复杂功能插槽并看到您的内容时,您应该准备好立即显示的内容。 If you don't, that could be why you're seeing the dashes instead of your intended text.如果不这样做,这可能就是您看到破折号而不是预期文本的原因。

  • Is your complication data source correctly assigned?您的并发症数据源是否正确分配? Under Targets > Your WatchKit Extension > Complications Configuration > Data Source Class , make sure ComplicationController is assigned.Targets > Your WatchKit Extension > Complications Configuration > Data Source Class ,确保 ComplicationController 已分配。

  • Your entry could be coming up nil if you're working on an older version of WatchOS.如果您使用的是旧版本的 WatchOS,您的条目可能为零。

EDIT - To clarify, graphicCornerComplication is just a property that I have added to some of my models so that I can quickly get a timeline entry by just calling graphicCornerComplication on them.编辑 - 澄清graphicCornerComplicationgraphicCornerComplication只是我添加到我的一些模型中的一个属性,这样我就可以通过对它们调用 graphicsCornerComplication 来快速获取时间线条目。 In use, it looks something like this:在使用中,它看起来像这样:

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
    switch complication.family {
    case .graphicCorner:
        let graphicCornerComplication = dataModel.graphicCornerComplication
        handler(graphicCornerComplication)
    default:
        handler(nil)
    }
}

Here's what worked for me:这是对我有用的:

  • Cut the original code with Cmd+X.使用 Cmd+X 剪切原始代码。
  • Change your view's code to contain just a simple Text.更改您的视图代码以仅包含一个简单的文本。
  • Build and run the complications.构建并运行并发症。
  • Paste your original code back.将您的原始代码粘贴回来。
  • Build and run again.再次构建并运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM