1

WidgetKit でウィジェットを作成しており、ウィジェットのコンテンツをクリック可能にしたいと考えています。たとえば、ユーザーが順位表をクリックした場合、アプリがアクティブになったときに順位表タブを開きたいと考えています。

アプリとウィジェットの間で通知を使用しようとしましたが、タップ ジェスチャが機能しません。タップ ジェスチャ内に印刷を追加しましたが、コンソールに表示されませんでした。また、両方に同じアプリ グループを追加しました。

WidgetView:

struct LargeWidget : View {
    
    @State var standings : [StandingsTable]
    
    var body: some View {
        VStack(alignment:.leading){
            if standings.count > 0{
                HStack(spacing:5){
                    Text("#").foregroundColor(.gray)
                        .frame(width: 30)
                    Text("Team".localized).foregroundColor(.gray)
                    Spacer()
                    Text("_D".localized).foregroundColor(.gray)
                        .frame(width: 30)
                    Text("_L".localized).foregroundColor(.gray)
                        .frame(width: 30)
                    Text("_W".localized).foregroundColor(.gray)
                        .frame(width: 30)
                    Text("_P".localized).foregroundColor(.gray)
                        .frame(width: 30)
                }
                Divider()
                ForEach(0..<5, id: \.self) { i in
                    HStack(spacing:5){
                        Text(standings[i].rank)
                            .font(.system(size: 15))
                            .padding(.vertical, 3)
                            .frame(width: 30)
                            .background(Color(UIColor.systemBackground))
                            .cornerRadius(4)
                        Text(standings[i].name)
                            .lineLimit(1)
                            .padding(.leading, 5)
                        Spacer()
                        Text(standings[i].drawn)
                            .frame(width: 30)
                        Text(standings[i].lost)
                            .frame(width: 30)
                        Text(standings[i].won)
                            .frame(width: 30)
                        Text(standings[i].points)
                            .frame(width: 30)
                    }
                    .padding(.vertical, 5)
                    .background(standings[i].name == "Besiktas" ? Color(UIColor.systemGray6) : Color.clear)
                    .cornerRadius(8)
                    
                }
                Spacer(minLength: 0)
            }else{
                Text("Large")
                    .padding()
            }
        }.padding()
        .onTapGesture {
            print("clicked to standings")
            DispatchQueue.main.async(execute: {
                NotificationCenter.default.post(name: NSNotification.Name("standings"), object: nil, userInfo: nil)
            })
        }
    }
}

ここでアプリのContentView:

import SwiftUI

extension NSNotification {
    static let openStandings = NSNotification.Name.init("standings")
}

struct ContentView: View {
    
    @State var show: Bool = false
    
    var body: some View {
        NavigationView{
            Text("Hello, world!")
                .padding()
        }.sheet(isPresented: self.$show) {
            VStack{
                Text("Notification")
                    .padding()
            }
        }
        .onReceive(NotificationCenter.default.publisher(for: NSNotification.openStandings))
        { obj in
            self.show.toggle()
        }
    }
}

ウィジェットのスクリーンショット

4

1 に答える 1