小编Liv*_*Liv的帖子

创建一个没有后退按钮的 NavigationLink SwiftUI

我试图链接 SomeView1() 中的按钮操作以导航到 someView2() 而没有屏幕顶部的后退按钮。相反,我想在 SomeView2() 中添加另一个按钮,该按钮将导航回 SomeView1()。这在 SwiftUI 中可能吗?

SomeView1()

struct SomeView1: View {
    var body: some View {
        NavigationView {
            VStack {
                //...view's content

                NavigationLink(destination: SomeView2()) {
                    Text("go to SomeView2")
                }
                Spacer()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

SomeView2()

struct SomeView2: View {
    var body: some View {
        NavigationView {
            VStack {
                //...view's content

                NavigationLink(destination: SomeView1()) {
                    Text("go to SomeView1")
                }
                Spacer()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是它的样子: 在此处输入图片说明

navigation swift swiftui

7
推荐指数
1
解决办法
3824
查看次数

从 SwiftUI 视图调用本地 UIViewController 函数

我正在尝试从 ContentView 调用本地 ViewController 函数。该函数使用了一些局部变量,不能移动到 ViewController 之外。

class ViewController: UIViewController {
    func doSomething() {...}
} 

extension ViewController : LinkViewDelegate {...}
Run Code Online (Sandbox Code Playgroud)

位于不同的文件中:

struct ContentView: View {

    init() {
        viewController = .init(nibName:nil, bundle:nil)
    }
    var viewController: viewController

var body: some View {
    Button(action: {self.viewController.doSomething()}) {
            Text("Link Account")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

UIViewController 不能更改为 UIViewRepresentable 之类的东西,因为 LinkViewDelegate 只能扩展 UIViewController。

view ios swift swiftui

6
推荐指数
3
解决办法
3473
查看次数

标签 统计

swift ×2

swiftui ×2

ios ×1

navigation ×1

view ×1