【问题标题】:How to add Context Menu into NavigationLink using SwiftUI?如何使用 SwiftUI 将上下文菜单添加到 NavigationLink?
【发布时间】:2021-07-19 09:31:38
【问题描述】:

我正在尝试使用以下代码将 contextMenu 添加到 NavigationLink:

   NavigationLink(recent.name, destination: TextEditing(isNew: false, fileName: recent.name)).contextMenu{Button("Rename") {
                   // code
                    }
                   }

,但我收到 swift 编译器错误:编译器无法在合理的时间内对该表达式进行类型检查;尝试将表达式分解为不同的子表达式

【问题讨论】:

    标签: ios swift xcode swiftui


    【解决方案1】:

    尝试将要执行的代码放在视图结构中的单独函数中,然后从上下文菜单中调用它:

    // put this in your view struct
    func action() {
        // code to run your menu action
    }
    

    然后从上下文菜单中调用它

    NavigationLink(recent.name, destination: TextEditing(isNew: false, fileName: recent.name)).contextMenu {
                    Button("Rename", action: action)
                }
    

    【讨论】:

      猜你喜欢
      • 2021-02-26
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 2017-12-09
      相关资源
      最近更新 更多