swift – 在视图控制器之间传递数据使用从导航控制器中嵌入的视图到tabbarcontroller的segue

我有两个视图,我想将数据从一个视图传递到下一个视图.第一个视图是我有数据,我想传递给下一个视图让我们称之为SourceViewController.但是SourceViewController嵌入在NavigationViewController中,secondViewController允许调用它,DestinationViewController是TabViewController中的firstView.

我试图使用this question的答案,它无法通过导航视图,它只是跳过整个逻辑.

这是我的代码

override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    if (segue.identifier == "loginSuccessSugue") {

        if let tab = self.presentingViewController as? UITabBarController,let nav = tab.viewControllers?[0] as? UINavigationController,let destinationVC = nav.viewControllers.first as? HomeViewController {


             destinationVC.currentBalance = serviceBalance
        }

    }
}

这是HomeViewController:

class HomeViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UICircularProgressRingDelegate{

var currentBalance = 0.0

 override func viewDidLoad() {
    super.viewDidLoad()

    circularBalance.maxValue = CGFloat(currentBalance)
     print(currentBalance)

   }

  override func viewDidAppear(_ animated: Bool) {
    print(currentBalance)
    circularBalance.setProgress(value: CGFloat(currentBalance),animationDuration: 3)

     }
}

这就是故事板的样子:

enter image description here

解决方法

这是我的视图控制器,您可以在其中检查我是否向tabbar第一个viewcontroller发送5:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        self.performSegue(withIdentifier: "segueIdentifier",sender: self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }


    // MARK: - Navigation

    // In a storyboard-based application,you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
        let barViewControllers = segue.destination as! UITabBarController
        let destinationNv = barViewControllers.viewControllers?[0] as! UINavigationController
        let destinationViewController = destinationNv.viewControllers[0] as! FirstViewController
        destinationViewController.currentBalance = 5
    }
}

现在您可以查看我的第一个视图控制器,您可以在哪里检查我们获得的值.

class FirstViewController: UIViewController {

    var currentBalance = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        print(currentBalance)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }

}

现在,您可以查看我的控制台和故事板:

enter image description here

enter image description here

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...