gpt4 book ai didi

ios - 从 SwiftUI View 到 UIKit ViewController 的 SwiftUI 不一致行为 "pushing"

转载 作者:行者123 更新时间:2023-12-04 13:31:43 25 4
gpt4 key购买 nike

我正在将一些 SwiftUI View 实现到现有的 UIKit 应用程序中。 DashboardView (下面)是一个主屏幕,然后我需要推送到现有的 UIKit ViewControllers .我正在尝试将 View Controller 推送到导航堆栈上(即使用后退按钮,而不是模态)。下面的代码,显示更多按钮下的推送按预期工作,推送到 SkateListViewController带有后退按钮,但是按下 WorkoutListView将 View Controller 呈现为模态?和WorkoutListView有关系吗?被嵌入到父 View 中?

struct DashboardView: View {
var body: some View {
ScrollView {
VStack(spacing: 7.0) {
HStack {
Text("Workouts")
.font(.custom(futuraBold, size: largeTitleTextSize))
.padding(.leading)
Spacer()

Button(action: {

let storyBoard: UIStoryboard = UIStoryboard(name: "Version3", bundle: nil)
let skateListTVC = storyBoard.instantiateViewController(withIdentifier: "skateListVC") as! SkateListTableViewController
UIApplication.topViewController()?.navigationController?.pushViewController(skateListTVC, animated: true) //This line works as expected and pushes onto Nav Stack with back button
}) {
Text("Show More")
.font(.custom(futuraMedium, size: headlineTextSize))
.foregroundColor(Color.blue)
}
}
ZStack {
VStack(alignment: .leading) {
WorkoutListView(workouts: [MockWorkout().getMockWorkout()])
}
.padding(.horizontal)
}
}
}
}
}
}


struct WorkoutListView: View {

@State var workouts: [HKWorkout]

var body: some View {
VStack(alignment: .leading) {
ForEach(workouts) { workout in
WorkoutRow(workout: workout)
.onTapGesture {
SelectedWorkoutSingleton.sharedInstance.selectedWorkout = workout
let storyBoard: UIStoryboard = UIStoryboard(name: "Version3", bundle: nil)
let skateDetailHostingControllerView = storyBoard.instantiateViewController(withIdentifier: "skateDetailHostingController") as! SkateDetailHostingController
UIApplication.topViewController()?.navigationController?.pushViewController(skateDetailHostingControllerView, animated: true) //This line shows Hosting Controller as Modal, doesn't push

}
}
}
}
}

最佳答案

为什么不使用 NavigationLink 然后推送到您的 View 。首先,创建 UIViewControllerRepresentable ..

struct SkateDetailWrapper: UIViewControllerRepresentable {
typealias UIViewControllerType = SkateDetailHostingController

func makeUIViewController(context: Context) -> SkateDetailHostingController {
let storyBoard: UIStoryboard = UIStoryboard(name: "Version3", bundle: nil)
let skateDetailHostingControllerView = storyBoard.instantiateViewController(withIdentifier: "skateDetailHostingController") as! SkateDetailHostingController
return skateDetailHostingControllerView
}

func updateUIViewController(_ uiViewController: SkateDetailHostingController, context: Context) {
}
}
然后用 NavigationLink 推送它:
ForEach(workouts, id:\.self) { workout in

NavigationLink(destination: SkateDetailWrapper()) {
WorkoutRow(workout: workout)
}

关于ios - 从 SwiftUI View 到 UIKit ViewController 的 SwiftUI 不一致行为 "pushing",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64667869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com