[英]Navigate to a UIViewController from inside a SwiftUIView
I have a UIKit
app where I have a SwiftUIView
hosted in a UIHostingController
, in this SwiftUIView I have a Button
which I would like to use to navigate to another UIViewController
when tapped.我有一个
UIKit
应用程序,其中我有一个SwiftUIView
托管在UIHostingController
中,在这个 SwiftUIView 中,我有一个Button
,我想在点击时导航到另一个UIViewController
。
How can I navigate to a UIViewController
from inside a SwiftUIView?如何从 SwiftUIView 中导航到
UIViewController
?
struct SwiftUIView: View {
var body: some View {
Button(action: {
// on tap, go to MyViewController
}) {
Text("Go to UIViewController")
}
}
}
Thanks谢谢
EDIT: Showing Code for Kyokook Hwang
(Option One).编辑:显示
Kyokook Hwang
的代码(选项一)。
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myLabel = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 21))
myLabel.text = "My Label"
view.addSubview(myLabel)
}
}
import SwiftUI
struct MyView: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<MyView>) -> MyViewController {
let vc = MyViewController()
return vc
}
func updateUIViewController(_ uiViewController: MyViewController, context: UIViewControllerRepresentableContext<MyView>) {
}
}
struct SomeView: View {
@State var isShowingMyVC = false
var body: some View {
VStack{
Button(action: {
self.isShowingMyVC = true
}) {
Text("Go to UIViewController")
.sheet(isPresented: $isShowingMyVC) {
MyView()
}
}
}
}
}
As far as I know, there are two way to achieve what you want to do.据我所知,有两种方法可以实现您想要做的事情。
1. Use UIViewControllerRepresentable
. 1.使用
UIViewControllerRepresentable
。
1) Create a bridge view with the protocol which embraces a UIViewController what you want to present. 1) 使用包含您想要呈现的 UIViewController 的协议创建一个桥视图。
2) Use .sheet
operator to present the bridge view when the button is touched. 2) 使用
.sheet
运算符在按钮被触摸时呈现桥视图。
There is an example below.下面有一个例子。
struct MyView: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<MyView>) -> MyViewController {
let vc = MyViewController()
return vc
}
func updateUIViewController(_ uiViewController: MyViewController, context: UIViewControllerRepresentableContext<MyView>) {
}
}
struct SwiftUIView: View {
@State var isShowingMyVC = false
var body: some View {
Button(action: {
// on tap, go to MyViewController
self.isShowingMyVC = true
}) {
Text("Go to UIViewController")
}
.sheet(isPresented: $isShowingMyVC) {
MyView()
}
}
}
2. Use a button action handler. 2. 使用按钮操作处理程序。
If you don't want SwiftUIView
to know any UIViewController that it should interact with, make SwiftUIView
have an action handler which is called by the button.如果您不希望
SwiftUIView
知道它应该与之交互的任何 UIViewController,请让SwiftUIView
有一个由按钮调用的操作处理程序。 and then, use it somewhere in UIKit code in order to present any UIViewController.然后,在 UIKit 代码中的某处使用它,以显示任何 UIViewController。
struct SwiftUIView: View {
@State var isShowingMyVC = false
var tapHandler: (() -> Void)? = nil
var body: some View {
Button(action: {
// on tap, go to MyViewController
self.tapHandler?()
}) {
Text("Go to UIViewController")
}
.sheet(isPresented: $isShowingMyVC) {
MyView()
}
}
}
let hostingView = UIHostingController(rootView: SwiftUIView() {
self.present(MyViewController(), animated: true, completion: nil)
})
hostingView.view.frame = view.bounds
view.addSubview(hostingView.view)
I'm not sure of those two ways are exactly what you are looking for.我不确定这两种方法是否正是您正在寻找的。 Just hope it's useful to you.
只是希望它对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.