【问题标题】:Pass Data From One TabbarVC to Another TabbarVC W/O losing Tabbar将数据从一个 TabbarVC 传递到另一个 TabbarVC 不丢失 Tabbar
【发布时间】:2017-08-29 05:23:49
【问题描述】:

我想将照片数据从 Tabbarcontroller 中的一个 Viewcontroller(cameraVC) 传递到同一 Tabbarcontroller 中的另一个 Viewcontroller(mainVC)在执行 segue 后不会丢失标签栏。

设置: *标签栏控制器

      -Navbarcontroller
      --mainVC (Tabbar Item)
      --cameraVC (Tabbar Item)

我必须能够在 cameraVC 中使用 prepareForSegue 和 performSegue(这就是我将数据传递给 mainVC 的方式)。

我所尝试的 W/O 成功: Keeping tab bar on View after segue?, Why TabBar hides after the segue?

当前实施:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let destinationNavigationController = segue.destination as! UINavigationController
    let targetController = destinationNavigationController.topViewController as! HomeVC

   // if let homeVC = segue.destination as? HomeVC {
       if let imageDict = sender as? Dictionary<String, Any> {
            let imageData = imageDict["snapshotData"]
            targetController.imageData = imageData as? Data

      // }
    }
}

@IBAction func sendBtnPressed(_ sender: Any) {

   performSegue(withIdentifier: "goFeed", sender: ["snapshotData": photoData.removeValue(forKey: "photoData")])

    imageTaken.image = nil
    self.view.insertSubview(previewView, aboveSubview: imageView)
}

【问题讨论】:

  • 需要传递多少图片数据?
  • 一次一张图片
  • 如果要传递单个图像,则无需将Data 传递给下一个 vc,只需将图像直接传递给下一个 vc 的类似对象即可。
  • 我必须传递数据,因为数据将用于发布到我的数据库

标签: ios swift uitabbarcontroller


【解决方案1】:

您错误地使用了performSegue(_:, sender:) 方法的sender 参数。它应该是启动 segue 的视图控制器。看起来您的 photoData 属性属于启动 segue 的视图控制器,因此只需在 prepareForSegue 方法中直接访问它。所以它看起来像这样:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let destinationNavigationController = segue.destination as! UINavigationController
    let targetController = destinationNavigationController.topViewController as! HomeVC

    if let imageData: Data = self.photoData["photoData"] as? Data {
        targetController.imageData = imageData
    }
}

@IBAction func sendBtnPressed(_ sender: Any) {

   performSegue(withIdentifier: "goFeed", sender: self)

    imageTaken.image = nil
    self.view.insertSubview(previewView, aboveSubview: imageView)
}

【讨论】:

  • 考虑到我需要将数据传递给 mainVC 以用作我的数据库的帖子,这不起作用。它也无助于维护 segue 上的标签栏
【解决方案2】:

我解决问题的方法是在我的 TabbarController 中创建一个变量,该变量可全局识别并用于将图像数据传递给 mainVC。这对我来说是避免创建重复视图的最可行的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2017-04-16
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多