简体   繁体   English

如何使用 SwiftUI 将上下文菜单添加到 NavigationLink 中?

[英]How to add Context Menu into NavigationLink using SwiftUI?

I'm trying to add contextMenu into NavigationLink, using this code:我正在尝试使用以下代码将 contextMenu 添加到 NavigationLink 中:

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

, but i receive swift compiler error: The compiler is unable to type-check this expression in reasonable time; ,但我收到 swift 编译器错误:编译器无法在合理的时间内对该表达式进行类型检查; try breaking up the expression into distinct sub-expressions尝试将表达式分解为不同的子表达式

Try putting your code to execute in a separate function in your view struct and then call it from the context menu:尝试将代码放在视图结构中的单独函数中执行,然后从上下文菜单中调用它:

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

And then call it from your context menu然后从上下文菜单中调用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SwiftUI 使用 NavigationLink 和 NavigationView - SwiftUI using NavigationLink and NavigationView 如何使用 Button Tap 移动 SwiftUI 中的视图或向 NavigationLink 添加操作? - How to move Views in SwiftUI with Button Tap or add an action to NavigationLink? 如何为通过 SwiftUI NavigationLink 推送的 UIViewController 添加标题? - How to add a title to a UIViewController that is pushed via a SwiftUI NavigationLink? 如何在SwiftUI中通过单击按钮(而不是通过使用NavigationLink)检查TextField的有效性? - How to check validation of TextField on Button Tap (not by using NavigationLink) in SwiftUI? 如何在没有 NavigationLink 的情况下使用 SwiftUI 推送新的根视图? - How to push a new root view using SwiftUI without NavigationLink? 如何在 SwiftUI 中使用 NavigationView 和 NavigationLink? - How to use NavigationView and NavigationLink in SwiftUI? 添加 NavigationLink 时出现 Swiftui 问题 - Swiftui issue when I add a NavigationLink 无法使用 NavigationLink + SwiftUI 导航到另一个屏幕 - Not able to navigate to another screen using NavigationLink + SwiftUI NavigationLink 与 SwiftUI:表达式类型不明确,没有更多上下文 - NavigationLink with SwiftUI: Type of expression is ambiguous without more context 如何使 NavigationLink 中的数据可滚动 swiftui - How to make data in NavigationLink scrollable swiftui
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM