swift WidgetKit -在编辑微件期间,是否有一种方法可以在意向处理中对数据进行分段

dfty9e19  于 2022-12-02  发布在  Swift
关注(0)|答案(1)|浏览(75)

这就是我们在WidgetKit中通过使用Intent定义文件提供“编辑小部件”功能的方式。

意图定义文件

编辑小部件期间的结果

为了提供一个选择列表,在Intent处理过程中(点击编辑小部件页面时),这是我们的代码。

import Intents

class IntentHandler: INExtension, ConfigurationIntentHandling {
    
    func provideWidgetItemOptionsCollection(for intent: ConfigurationIntent, with completion: @escaping (INObjectCollection<WidgetItem>?, Error?) -> Swift.Void) {
        var widgetItems = [WidgetItem]()
        
        widgetItems.append(WidgetItem(identifier: "1", display: "Visit grandma"))
        widgetItems.append(WidgetItem(identifier: "2", display: "Don't forget milk"))
        widgetItems.append(WidgetItem(identifier: "3", display: "Buy socks"))
        widgetItems.append(WidgetItem(identifier: "4", display: "Pick up laundry"))
        widgetItems.append(WidgetItem(identifier: "5", display: "Reply email"))
        widgetItems.append(WidgetItem(identifier: "6", display: "Meeting at 3pm"))
        
        let collection = INObjectCollection(items: widgetItems)
        
        completion(collection, nil)
    }
    
    override func handler(for intent: INIntent) -> Any {
        // This is the default implementation.  If you want different objects to handle different intents,
        // you can override this and return the handler you want for that particular intent.
        
        return self
    }
    
}

这就是

意向处理期间的结果

我想知道,有没有办法在UI上创建多个部分?
例如,

[Personal]
"Visit grandma"
"Don't forget milk"
"Buy socks"
"Pick up laundry"

[Work]
"Reply email"
"Meeting at 3pm"
  • 谢谢-谢谢
kupeojn6

kupeojn61#

这就是解决办法。

import Intents

class IntentHandler: INExtension, ConfigurationIntentHandling {
    
    func provideWidgetItemOptionsCollection(for intent: ConfigurationIntent, with completion: @escaping (INObjectCollection<WidgetItem>?, Error?) -> Swift.Void) {
        
        var personalWidgetItems = [WidgetItem]()
        var workWidgetItems = [WidgetItem]()
        
        personalWidgetItems.append(WidgetItem(identifier: "1", display: "Visit grandma"))
        personalWidgetItems.append(WidgetItem(identifier: "2", display: "Don't forget milk"))
        personalWidgetItems.append(WidgetItem(identifier: "3", display: "Buy socks"))
        personalWidgetItems.append(WidgetItem(identifier: "4", display: "Pick up laundry"))
        workWidgetItems.append(WidgetItem(identifier: "5", display: "Reply email"))
        workWidgetItems.append(WidgetItem(identifier: "6", display: "Meeting at 3pm"))
        
        let personalSection = INObjectSection(title: "Personal", items: personalWidgetItems)
        let workSection = INObjectSection(title: "Work", items: workWidgetItems)
        
        let collection = INObjectCollection(sections: [personalSection, workSection])
        
        completion(collection, nil)
    }
    
    override func handler(for intent: INIntent) -> Any {
        // This is the default implementation.  If you want different objects to handle different intents,
        // you can override this and return the handler you want for that particular intent.
        
        return self
    }
    
}

,结果如下。

相关问题