我正在学习SwiftUI,目前的重点是实现一种方法,该方法可以使用UIKit来实现。我需要创建一个方法,根据布尔值的值确定是否要推送视图或以模态方式呈现视图。
在UIKit中,我的代码如下:
```swift if shouldPush { navigationController?.pushViewController(viewController, animated: true) } else { present(viewController, animated: true) } ```
var presentVC = true // boolean that determines whether VC will be presented or pushed
let vc = ViewController() //Your VC that will be pushed or presented
if (presentVC == true) {
self.presentViewController(vc, animated: true, completion: nil)
} else {
self.navigationController.pushViewController(vc, animated: true)
}
但在SwiftUI中,我不确定如何使用以下内容正确地实现:
- NavigationLink - 用于推送视图
- .sheet(isPresented:,content:) - 用于以模态方式呈现视图
似乎NavigationLink和.sheet修饰符与视图实现耦合。 有没有人在SwiftUI中遇到过并解决了这种情况?谢谢
我正在使用SwiftUI 1.0,因为我需要支持iOS 13。