You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在TableViewController的UIAction中打开SwiftUI的PictureView?

实现通过UIAction打开SwiftUI视图(PictureView)

核心实现方案

由于你是在UIKit的TableViewController中调用SwiftUI编写的PictureView,需要借助UIHostingController将SwiftUI视图包装为UIKit兼容的视图控制器,再执行跳转操作。

修改你的actionNext代码如下:

let actionNext = UIAction(title: "Next", image: UIImage(systemName: "arrow.forward.circle.fill")) { [weak self] action in
    // 用UIHostingController包裹SwiftUI视图
    let pictureVC = UIHostingController(rootView: PictureView())
    // 若当前控制器在导航栈中,使用push跳转
    self?.navigationController?.pushViewController(pictureVC, animated: true)
    // 若无需导航结构,可改用present弹出模态视图
    // self?.present(pictureVC, animated: true)
}

关键注意事项

  • 添加[weak self]避免循环引用,防止内存泄漏
  • 根据页面结构选择跳转方式:导航栈内优先用push,独立页面用present

内容的提问来源于stack exchange,提问作者EN_Tech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 04:35:28