ios - 使用推送弹出导航在多个 View Controller 之间传递数据时出现问题

标签 ios swift xcode

我的工作是
1 .从第一个 View Controller 传递数据 -> 使用 pushViewController 的第二个 View Controller :成功

2 .Passing data from second view controller -> First View Controller using popViewController: Success//使用委托(delegate)协议(protocol)返回数据

3 .从第三个 View Controller 传递数据 -> 第一个 View Controller :错误

代码 :
ViewController.swift

import UIKit

class ViewController: UIViewController,secondViewDelegate,thirddelegate {

    @IBOutlet var Firstoutput: UILabel!

    @IBOutlet var inputField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func popdata(value: String) { //second view controller delegate protocol defintions
        Firstoutput.text = "\(value)"
    }
    func thirdView(datas:String) //third view controller delegate protocol definition
    {
        print(datas)
    }
    @IBAction func pushBtn(_ sender: Any) { //first view button action

      let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let vcFirst = storyboard.instantiateViewController(withIdentifier: "second") as! SecondViewController //secondview

        vcFirst.Secondtext = inputField.text

        vcFirst.delegate = self //second view delegate intiate

        let vcThird = storyboard.instantiateViewController(withIdentifier: "third") as! ThirdViewController //third view

        vcThird.thirddelegate = self //third view delegate intiate

        navigationController?.pushViewController(vcFirst, animated: true)
    }
}

SecondViewController.swift
import UIKit
//protocol for passing data when pop
protocol secondViewDelegate {
    func popdata(value:String)
}
class SecondViewController: UIViewController {

    @IBOutlet var secondOutputField: UILabel!

    @IBOutlet var secondInputField: UITextField!

    var Secondtext:String!

    var delegate:secondViewDelegate!
    override func viewDidLoad() {
        super.viewDidLoad()
        secondOutputField.text = Secondtext

    }
    //popping back to first view controller using data
    @IBAction func popBtn(_ sender: Any) {
        if delegate?.popdata != nil
        {
        delegate.popdata(value: secondInputField.text!)
        }
    navigationController?.popViewController(animated: true)
    }
    //pushing to third view controller
    @IBAction func thirdPageBtn(_ sender: Any) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vcThird = storyboard.instantiateViewController(withIdentifier: "third") as! ThirdViewController
        navigationController?.pushViewController(vcThird, animated: true)
    }

}

ThirdViewController.swift
import UIKit
protocol thirdviewDelegate //delegate to pass data from thirs view to first view
{
    func thirdView(datas:String)
}
class ThirdViewController: UIViewController {

    var thirddelegate:thirdviewDelegate!

    @IBOutlet var thirdTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad
    }


    @IBAction func thirdViewInvoke(_ sender: Any) {
        if thirddelegate?.thirdView != nil
        {
        thirddelegate.thirdView(datas: thirdTextField.text!)
        }
    }


}

输出截图:
Pushing data no problem
Popping data no problem
Pass data from Third to First view Controller but its not passing value

仅将数据从第三个 View Controller 传递到第一个 View Controller 不起作用帮助我解决这个问题...谢谢

最佳答案

您可以使用 NotificationCenter接收数据。

按下 ThirdViewController 中的获取数据按钮,发布您的notification .

let myDataToPass : [String: Any] = ["myData" : yourStringValue]
NotificationCenter.default.post(name: Notification.Name("getDataPressed"), object: myDataToPass)

在您的 FirstViewController , 在 viewDidLoad 中添加观察者将监听 notification :
override func viewDidLoad() {
    super.viewDidLoad()
      NotificationCenter.default.addObserver(self, selector: #selector(receiveInitialDetails(notification:)), name: Notification.Name("getDataPressed"), object: nil)
}

//once the notification is received, this function will be called and you can get the data that you passed in thirdVC using notification.userInfo
@objc func receiveInitialDetails(notification : Notification) {
     let data = notification.userInfo as! [String:Any]
     let yourStringValue = data["myData"] as! String
     // you have your data here. downcast it to your desired data type.
}

关于ios - 使用推送弹出导航在多个 View Controller 之间传递数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60163130/

相关文章:

xcode - NSUserDefaults、UISwitch 和设置默认值

ios - 从 iOS 11 发布到 iOS 10 后,是否可以降低 iOS 应用程序的最低部署目标

ios - 来自 Visual Studio 的 Xamarin.ios Today 扩展

ios - WKInterfacePicker 列表中的图片样式

ios - 是否可以在 AKAudioPlayer 上使用 AKAmplitudeTracker 并测量振幅变化?

swift 3 : issue with AVVideoCompositionCoreAnimationTool to add watermark on video

ios - 请求被服务委托(delegate) (SBMainWorkspace) 拒绝,原因为 : Unspecified

iphone - 如何将核心数据添加到 iOS 静态框架?

我的 Mac App Store 中的 Xcode 4 显示已安装,但未安装

xcode - 如何将私有(private)程序分发到 100 多个设备?