使用 SwiftUI 按钮从 SwiftUI 返回 Storyboard
为了合并Swiftui的视图和故事板,我已经创建了一个从故事板到Swiftui视图的Segue,但目前无法回来。
我不想通过导航栏进行此操作,而不是我的SwiftUi视图中的按钮,该按钮将segue放回到主板视图中。
到目前为止,我实现的结果是将SwiftUI视图用作托管控制器,然后将视图控制器的SEGUE添加到子托管控制器中。
我要放松的按钮看起来像这样:
Button(action: {
print("button pressed") // UNWIND SEGUE HERE
}) {
Image("TopLeft")
.renderingMode(.original)
.resizable()
.scaledToFit()
.frame(width: 15, height: 15)
}
}
此代码链接到称为secondview
的类,即SwiftUI代码。第二视图是由一个子类Child -HostingController激活的,该子类链接到情节提要上的UihostingController:
class ChildHostingController: UIHostingController<SecondView> {
required init?(coder: NSCoder) {
super.init(coder: coder,rootView: SecondView());
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
是否有可能有任何方法可以放松segue回头查看控制器?
非常感谢!
In an attempt to merge a SwiftUI view and Storyboard I have created a segue from a storyboard into a SwiftUI view, but currently have no way of getting back.
I don't want to do this through a navigation bar, rather a button in my SwiftUI view which unwinds the segue back to the main.storyboard view.
The way I have achieved my outcome so far is by using the SwiftUI view as a hosting controller, and then added a segue from the View Controller to the Child Hosting Controller.
The button which I want to unwind the segue looks like this:
Button(action: {
print("button pressed") // UNWIND SEGUE HERE
}) {
Image("TopLeft")
.renderingMode(.original)
.resizable()
.scaledToFit()
.frame(width: 15, height: 15)
}
}
This code is linked to a class called SecondView
which is the SwiftUI code. SecondView is activated by a subclass ChildHostingController, linked to the UIHostingController on the storyboard:
class ChildHostingController: UIHostingController<SecondView> {
required init?(coder: NSCoder) {
super.init(coder: coder,rootView: SecondView());
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Is there any possible way to unwind the segue back to View Controller?
Much appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种可能的方法是通过视图模型将视图控制器的放松操作注入封闭,并将其称为SwiftUI视图按钮中。
这是主要部分:
完整
A possible approach is to inject a closure with unwind action from a view controller via view model into SwiftUI view and call it inside SwiftUI view button.
Here is main part:
Complete findings and code here
据我所知,您必须通过主故事板创建Undind egues。但是,一旦完成并给出了标识符,您可以修改
uihostingController
,如下所示:在
secondview
中,您需要定义该divive> dimplastview controller < /代码>方法:
然后用您的按钮调用它:
As far as I know you must create unwind segues via your main storyboard. However, once you've done that and given it an identifier, you could modify your
UIHostingController
as follows:In
SecondView
you'd want to define thatdismissViewController
method:Then call it with your button: