如何从另一个类更改TabView中的选定选项卡?

时间:2019-10-07 00:25:27

标签: swift swiftui swift5

我正在尝试根据收到的Push通知在TabView中切换选项卡。我的TabView看起来像这样:

import SwiftUI
import Combine

struct ContentView: View {
    @State var selection = 0

    var body: some View {
        TabView(selection: $selection){

接收推送通知并尝试更改选项卡:

class SceneDelegate: UIResponder, UIWindowSceneDelegate, UNUserNotificationCenterDelegate {
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void) {

        (window?.rootViewController?.view as? ContentView)?.selection = 2

这会产生错误-Cannot assign to immutable expression of type 'Int'。我该如何解决?

0 个答案:

没有答案