在长按手势时导航到另一个SwiftUI屏幕。

huangapple go评论242阅读模式
英文:

On onLongPressGresture navigate to another SwiftUI screen

问题

以下是翻译好的部分:

Parent View Controller which is UIKit and has tableView
when user will click on specific row I am showing swiftUI screen which is Class A

父视图控制器是UIKit,并且具有表视图,当用户点击特定行时,我会显示SwiftUI屏幕,这是Class A。

Parent class to Class A navigation code is

父类到Class A的导航代码如下:

  1. let classA = ClassA()
  2. let navigateToClassA = UIHostingController(rootView: classA)
  3. self.navigationController?.pushViewController(navigateToClassA, animated: true)

Now I am landing to Class A and below is Class A code

现在我已经进入了Class A,以下是Class A的代码

  1. var body: some View {
  2. VStack(alignment: .leading, spacing: 7) {
  3. Text("user 1")
  4. Text("长按这里")
  5. .onLongPressGesture(minimumDuration: 2) {
  6. // 在这里添加导航代码以导航到其他的SwiftUI屏幕
  7. }
  8. Text("User 2")
  9. }
  10. .navigationTitle("Class A")
  11. }

this is my Class B code which will show when I will navigate from onLongPressGesture in Class A

这是我的Class B代码,当我从Class A的onLongPressGesture中导航时将显示它

  1. struct ClassB: View {
  2. var body: some View {
  3. VStack(alignment: .leading){
  4. Text("你好")
  5. }
  6. .navigationTitle("Class B")
  7. }
  8. }

请注意,我将Class B的名称更改为"ClassB",以便在SwiftUI中使用。

英文:

I have 3 Text in SwiftUI (in UIKit UILabel) , I need to navigate to other screen when I use onLongPressGresture in one of my Text, below is the code.

Parent View Controller which is UIKit and has tableView
when user will click on specific row I am showing swiftUI screen which is Class A

Parent class to Class A navigation code is

  1. let classA = ClassA()
  2. let navigateToClassA = UIHostingController(rootView: classA)
  3. self.navigationController?.pushViewController(navigateToClassA, animated: true)

Now I am landing to Class A and below is Class A code

  1. var body: some View {
  2. VStack(alignment: .leading, spacing: 7) {
  3. Text("user 1")
  4. Text("Long Press Here")
  5. .onLongPressGesture(minimumDuration: 2) {
  6. (I need to add navigation code here so I will navigate to other SwiftUI screen)
  7. }
  8. Text("User 2")
  9. }
  10. .navigationTitle("Class A")
  11. }

this is my Class B code which will show when I will navigate from onLongPressGesture in Class A

  1. struct classB: View {
  2. var body: some View {
  3. VStack(alignment: .leading){
  4. Text("Hello")
  5. }
  6. .navigationTitle("Class B")
  7. }
  8. }
  9. </details>
  10. # 答案1
  11. **得分**: 4
  12. ```swift
  13. 我会做这样的事情:
  14. 结构体 ContentView: View {
  15. @State private var isChildViewPresented: Bool = false
  16. var body: some View {
  17. NavigationStack {
  18. VStack(alignment: .leading, spacing: 7) {
  19. Text("用户 1")
  20. Text("长按这里")
  21. .onLongPressGesture(minimumDuration: 2) {
  22. isChildViewPresented = true
  23. }
  24. Text("用户 2")
  25. }
  26. .padding()
  27. .navigationDestination(isPresented: $isChildViewPresented) {
  28. Text("子视图")
  29. }
  30. }
  31. }
  32. }
  33. 基本上,你需要将所有内容包裹在 `NavigationStack` 中,我个人更喜欢它而不是 `NavigationView`,因为后者将会过时。
  34. 你可以利用状态变量 `isChildViewPresented`,并在希望执行导航时改变其状态。
  35. 通过使用 `.navigationDestination`,你指定了在 `isChildViewPresented` 的状态切换时要导航到哪里。
  36. <details>
  37. <summary>英文:</summary>
  38. I would do something like this:
  39. struct ContentView: View {
  40. @State private var isChildViewPresented: Bool = false
  41. var body: some View {
  42. NavigationStack {
  43. VStack(alignment: .leading, spacing: 7) {
  44. Text(&quot;user 1&quot;)
  45. Text(&quot;Long Press Here&quot;)
  46. .onLongPressGesture(minimumDuration: 2) {
  47. isChildViewPresented = true
  48. }
  49. Text(&quot;User 2&quot;)
  50. }
  51. .padding()
  52. .navigationDestination(isPresented: $isChildViewPresented) {
  53. Text(&quot;ChildView&quot;)
  54. }
  55. }
  56. }
  57. }
  58. Basically you need to wrap everything inside e `NavigationStack`, i would personally prefer it over the `NavigationView` since the latter one is going to be obsolete.
  59. You make use of the state variable `isChildViewPresented`, and change its state when you want to perform the navigation.
  60. By making use of the `.navigationDestination` you specify it where to navigate to when the state of the `isChildViewPresented` is toggled
  61. </details>

huangapple
  • 本文由 发表于 2023年8月11日 00:41:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定