使用 SwiftUI 按钮从 SwiftUI 返回 Storyboard

发布于 2025-01-20 22:12:02 字数 1046 浏览 1 评论 0原文

为了合并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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅人 2025-01-27 22:12:02

一种可能的方法是通过视图模型将视图控制器的放松操作注入封闭,并将其称为SwiftUI视图按钮中。

这是主要部分:

super.init(coder: coder, rootView: SecondView(vm: viewModel))
viewModel.unwind = { [weak self] in
   // call unwind segue created in storyboard
    self?.performSegue(withIdentifier: "unwind_segue_identifier", sender: self)
}

完整

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:

super.init(coder: coder, rootView: SecondView(vm: viewModel))
viewModel.unwind = { [weak self] in
   // call unwind segue created in storyboard
    self?.performSegue(withIdentifier: "unwind_segue_identifier", sender: self)
}

Complete findings and code here

萌酱 2025-01-27 22:12:02

据我所知,您必须通过主故事板创建Undind egues。但是,一旦完成并给出了标识符,您可以修改uihostingController,如下所示:

class ChildHostingController: UIHostingController<SecondView> {

    func dismiss() {
        performSegue(withIdentifier: "myUnwindSegueIdentifier", sender: self)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder,rootView: SecondView())
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.rootView.dismissViewController = dismiss
    }
}

secondview中,您需要定义该divive> dimplastview controller < /代码>方法:

var dismissViewController: (() -> Void)?

然后用您的按钮调用它:

Button(action: {
    dismissViewController!()

  }) {
      Image("TopLeft")
      .renderingMode(.original)
      .resizable()
      .scaledToFit()
      .frame(width: 15, height: 15)
  }
}

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:

class ChildHostingController: UIHostingController<SecondView> {

    func dismiss() {
        performSegue(withIdentifier: "myUnwindSegueIdentifier", sender: self)
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder,rootView: SecondView())
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.rootView.dismissViewController = dismiss
    }
}

In SecondView you'd want to define that dismissViewController method:

var dismissViewController: (() -> Void)?

Then call it with your button:

Button(action: {
    dismissViewController!()

  }) {
      Image("TopLeft")
      .renderingMode(.original)
      .resizable()
      .scaledToFit()
      .frame(width: 15, height: 15)
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文