我试图链接 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)
我正在尝试从 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。