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

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

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的导航代码如下:

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

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

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

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

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

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

struct ClassB: View {

var body: some View {
VStack(alignment: .leading){
     Text("你好")
  }
  .navigationTitle("Class B")
 }
}

请注意,我将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

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

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

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

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

struct classB: View {

var body: some View {
VStack(alignment: .leading){
     Text("Hello")
  }
  .navigationTitle("Class B")
 }
}

</details>


# 答案1
**得分**: 4

```swift
我会做这样的事情:

     结构体 ContentView: View {
            @State private var isChildViewPresented: Bool = false
            var body: some View {
                NavigationStack {
                    VStack(alignment: .leading, spacing: 7) {
                        Text("用户 1")
                        Text("长按这里")
                            .onLongPressGesture(minimumDuration: 2) {
                                isChildViewPresented = true
                            }
                        Text("用户 2")
                    }
                    .padding()
                    .navigationDestination(isPresented: $isChildViewPresented) {
                        Text("子视图")
                    }
                }
            }
        }

基本上,你需要将所有内容包裹在 `NavigationStack` 中,我个人更喜欢它而不是 `NavigationView`,因为后者将会过时。
你可以利用状态变量 `isChildViewPresented`,并在希望执行导航时改变其状态。
通过使用 `.navigationDestination`,你指定了在 `isChildViewPresented` 的状态切换时要导航到哪里。

<details>
<summary>英文:</summary>

I would do something like this:

   

     struct ContentView: View {
            @State private var isChildViewPresented: Bool = false
            var body: some View {
                NavigationStack {
                    VStack(alignment: .leading, spacing: 7) {
                        Text(&quot;user 1&quot;)
                        Text(&quot;Long Press Here&quot;)
                            .onLongPressGesture(minimumDuration: 2) {
                                isChildViewPresented = true
                            }
                        Text(&quot;User 2&quot;)
                    }
                    .padding()
                    .navigationDestination(isPresented: $isChildViewPresented) {
                        Text(&quot;ChildView&quot;)
                    }
                }
            }
        }

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. 
You make use of the state variable `isChildViewPresented`, and change its state when you want to perform the navigation.
By making use of the `.navigationDestination` you specify it where to navigate to when the state of the `isChildViewPresented` is toggled

</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:

确定