“我的窗口小部件”从UserDefaults/ Appgroups
加载一些数据,并根据它显示一些文本和图片。
这是从头开始的。
如果我更改UserDefaults并使用WidgetCenter.shared.reloadAllTimelines()
,则仅更改图片而不更改文本。
我该怎么办?还有其他选项可以在主应用程序和小部件之间共享数据吗?似乎是一个错误。
这是我的Widgetentry:
struct Provider: IntentTimelineProvider {
let networkManager = NetworkManager()
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), clubnamehome: "test", clubnameaway: "test2")
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
networkManager.fetchData { (post) in
let entry = SimpleEntry(date: Date(), configuration: configuration, clubnamehome: post.home_name, clubnameaway: post.away_name)
completion(entry)
}
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
networkManager.fetchData { (post) in
let entries = [ SimpleEntry(date: Date(), configuration: configuration, clubnamehome: post.home_name, clubnameaway: post.away_name) ]
let reloadTime = Calendar.current.date(byAdding: .minute, value: 5, to: Date())!
let timeline = Timeline(entries: entries, policy: .after(reloadTime))
completion(timeline)
}
}
}
在这里我设置UserDefault的值:
@IBAction func losGehts(_ sender: UIButton) {
switch team {
case "arminia":
UserDefaults(suiteName: "...")!.set("114", forKey: "tabellenId")
if #available(iOS 14.0, *) {
WidgetCenter.shared.reloadAllTimelines()
} else {
// Fallback on earlier versions
}
case "augsburg":
这是我的看法:
struct MyTeamWidgetEntryView : View {
@Environment(\.colorScheme) var colorScheme
var imageIcon = "stuttgart"
var entry: Provider.Entry
let logo = UserDefaults(suiteName: "...")!.string(forKey: "logo")
let mannschaftsName = UserDefaults(suiteName: "...")!.string(forKey: "teamname")
let teamID = "289"
var body: some View {
HStack {
Spacer()
VStack (alignment: .leading, spacing: 0) {
Spacer().frame(height: 10)
// image von hier
HStack {
Spacer()
switch logo {
case "arminia":
Image("bundesliga1/arminia").resizable().aspectRatio(contentMode: .fit).frame(width: 25, height: 25, alignment: .bottomLeading)
case "augsburg":
Image("bundesliga1/augsburg").resizable().aspectRatio(contentMode: .fit).frame(width: 25, height: 25, alignment: .bottomLeading)
default:
Image("bundesliga1/...").resizable().aspectRatio(contentMode: .fit).frame(width: 90, height: 90, alignment: .center)
}
Spacer()
}
// image bis hier
Spacer().frame(height: 5)
}
Spacer()
VStack (alignment: .leading, spacing: 6) {
VStack {
HStack{
Text(entry.clubnamehome).font(.system(size: 10)).bold()
Text(":").font(.system(size: 10)).bold()
Text(entry.clubnameaway).font(.system(size: 10)).bold()
Spacer()
//überlegen ob spacer rein soll oder nicht
}
}
}
}
}
答案 0 :(得分:1)
您似乎从应用组UserDefaults中正确读取了logo
和mannschaftsName
值:
struct MyTeamWidgetEntryView : View {
let logo = UserDefaults(suiteName: "...")!.string(forKey: "logo")
let mannschaftsName = UserDefaults(suiteName: "...")!.string(forKey: "teamname")
现在,您需要确保在主应用程序中正确设置了这些值。在您发布的代码中,您只能为tabellenId
键设置一个值:
case "arminia":
UserDefaults(suiteName: "...")!.set("114", forKey: "tabellenId")
您还需要更新logo
和mannschaftsName
:
case "arminia":
UserDefaults(suiteName: "...")!.set("114", forKey: "tabellenId")
UserDefaults(suiteName: "...")!.set("???", forKey: "logo")
UserDefaults(suiteName: "...")!.set("???", forKey: "mannschaftsName")
这可能是摘录的,所以您不要重复自己:
switch team {
...
}
UserDefaults(suiteName: "...")!.set(team, forKey: "mannschaftsName")