Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用现有导航控制器从swiftui导航到viewcontroller_Ios_Swift_Swiftui_Uikit - Fatal编程技术网

Ios 如何使用现有导航控制器从swiftui导航到viewcontroller

Ios 如何使用现有导航控制器从swiftui导航到viewcontroller,ios,swift,swiftui,uikit,Ios,Swift,Swiftui,Uikit,我有一个导航控制器,导航到视图控制器,导航到uihostingcontroller。如何从swift ui推送到另一个视图控制器 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) els

我有一个导航控制器,导航到视图控制器,导航到uihostingcontroller。如何从swift ui推送到另一个视图控制器

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        self.window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.windowScene = windowScene
        let navigationView = UINavigationController(rootViewController: PreviewViewVideoController())
        navigationView.isToolbarHidden = true
        self.window?.rootViewController = navigationView
        self.window?.makeKeyAndVisible()
    }
在预览视频控制器

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch cards[indexPath.row] {
        case .trans_human:
            let controller = UIHostingControllerCustom(rootView: FilmOverviewView(overview: TransHumanOverview()))
            self.navigationController?.pushViewController(controller, animated: true)
            controller.navigationItem.title = cards[indexPath.row].rawValue
}
}
class UIHostingControllerCustom<V: View>: UIHostingController<V> {
    override func viewWillAppear(_ animated: Bool) { 
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
}
在电影概览中

struct FilmOverviewView: View {
    var filmOverview: FilmOverview!
    var imageResource: String!
    
    init(overview: FilmOverview) {
        filmOverview = overview
        
    }

var body: some View {
    ScrollView(Axis.Set.vertical, showsIndicators: false) {
        VStack {
            Image(filmOverview.imageResource)
                .resizable()
                .frame(width: UIScreen.main.bounds.width,
                       height: UIScreen.main.bounds.height / 2)
                .overlay(StartButtonOverlay(), alignment: .bottom)
            Button(action: {})

                
}
}

如何使用现有导航堆栈从swiftui按钮视图操作导航到新的视图控制器

我将通过环境值将
UINavigationController
注入swiftui堆栈

struct NavigationControllerKey: EnvironmentKey {
    static let defaultValue: UINavigationController? = nil
}

extension EnvironmentValues {
    var navigationController: NavigationControllerKey.Value {
        get {
            return self[NavigationControllerKey.self]
        }
        set {
            self[NavigationControllerKey.self] = newValue
        }
    }
}
然后在表视图中委托注入它

let controller = UIHostingControllerCustom(rootView: 
      FilmOverviewView(overview: TransHumanOverview()
         .environment(\.navigationController, self.navigationController))
现在在
FilmOverview
中,我们可以使用它

struct FilmOverviewView: View {
   @Environment(\.navigationController) var navigationController
   // ... other code

   var body: some View {
       ScrollView(Axis.Set.vertical, showsIndicators: false) {
          VStack {
            // ... other code

            Button("Demo") {
               let controller = UIHostingControllerCustom(rootView: 
                  SomeOtherView()   // inject if needed again
                     .environment(\.navigationController, self.navigationController)))
               self.navigationController?.pushViewController(controller, animated: true)
            }
   }
}
更新:更新了自定义主机控制器

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch cards[indexPath.row] {
        case .trans_human:
            let controller = UIHostingControllerCustom(rootView: FilmOverviewView(overview: TransHumanOverview()))
            self.navigationController?.pushViewController(controller, animated: true)
            controller.navigationItem.title = cards[indexPath.row].rawValue
}
}
class UIHostingControllerCustom<V: View>: UIHostingController<V> {
    override func viewWillAppear(_ animated: Bool) { 
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
}

类UIHostingController自定义:UIHostingController{ 覆盖函数视图将出现(u动画:Bool){ navigationController?.setNavigationBarHidden(真,动画:假) } }
SwiftUI不能与UINavigationController一起使用,因此您需要继续使用UIKit在这种情况下进行导航。我如何通过SwiftUI中的按钮进行导航。我对代码进行了编辑。假设我有一个按钮,你将如何从它的actionlet controller=UIHostingControllerCustom(rootView:FilmOverview(overview:TransHumanOverview()).environment(\.navigationController,self.navigationController))中进行编辑在表视图中,无法将“某些视图”类型的值转换为预期的参数类型“FilmOverviewView”。您需要更正自定义
UIHostingControllerCustom中的泛型{navigationController?.setNavigationBarHidden(true,动画:false)}}我需要在此处更改什么查看更新的部件