【问题标题】:Is there a way to add an extra function to NavigationLink? SwiftUI有没有办法向 NavigationLink 添加额外的功能? SwiftUI
【发布时间】:2023-03-14 11:15:01
【问题描述】:

我想为 NavigationLink 添加一个额外的功能。

示例代码是这样的:

struct ContentView: View {

func yes () {
print("yes")
}

var body: some View {

NavigationView {
NavigationLink(destination: level1()) {

     Text("Next")      
}}}}

我知道这行不通,但是可以做这样的事情吗? (会去目的地,同时调用函数)

NavigationLink(destination: level1(), yes()) {Text("Next")}   

我尝试在 NavigationLink 中放置一个按钮,但它也不起作用。当我这样做时,只有按钮中的功能起作用,NavigationLink 不起作用。

NavigationLink(destination: level1())   {
        Button(action: { self.yes() }) 
        { Text("Button")}
        }

【问题讨论】:

  • 你不能将方法作为闭包传递给 init 中的目标视图

标签: function navigation swiftui navigationlink


【解决方案1】:

使用onAppear(perform:)。这将在出现View 时执行一些功能。

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailView().onAppear {
                self.someFunc()
            }) {
                Text("First Screen")
            }
        }
    }

    func someFunc() {
        print("Click")
    }
}

struct DetailView: View {
    var body: some View {
        Text("Second Screen")
    }
}

【讨论】:

  • 你说得对,我可以在 NavigationLink 和 DetailView 中使用 onAppear。谢谢!
  • NavigationLink 上的 .onAppear 是正确的。这是对许多其他过于复杂的解决方案提出的在视图上执行和操作的简单解决方案。在某种程度上,这是一种在 Swift 下获得 viewdidappear 的方式。
  • 在 iOS 15 中,我遇到了一些小故障 - 导航视图以编程方式点击后退按钮并重新排序列表元素。我不能推荐解决方案
  • @SevenDays 可能是。此解决方案最初适用于 iOS 13。请告诉我,哪种方式更适合 iOS 15?
  • @AlekseyPotapov 我发布了这个问题的解决方案。
【解决方案2】:

SwiftUI、iOS 15.1 和 Xcode 13.1 的解决方案。

import Foundation
import SwiftUI

// NavigationLink replacement with action
// Usage:
//var body: some View {
//  NavigationButton(
//      action: { print("tapped!") },
//      destination: { Text("Pushed View") },
//      label: { Text("Tap me") }
//  )
//}

struct NavigationButton<Destination: View, Label: View>: View {
        var action: () -> Void = { }
        var destination: () -> Destination
        var label: () -> Label

        @State private var isActive: Bool = false

        var body: some View {
                Button(action: {
                        self.action()
                        self.isActive.toggle()
                }) {
                        self.label()
                            .background(
                                ScrollView { // Fixes a bug where the navigation bar may become hidden on the pushed view
                                        NavigationLink(destination: LazyDestination { self.destination() },
                                                                                                 isActive: self.$isActive) { EmptyView() }
                                }
                            )
                }
        }
}

// This view lets us avoid instantiating our Destination before it has been pushed.
struct LazyDestination<Destination: View>: View {
        var destination: () -> Destination
        var body: some View {
                self.destination()
        }
}

我找不到原始代码,但这个解决方案非常适合我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    相关资源
    最近更新 更多