我的工作是
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!)
}
}
}
输出截图:
仅将数据从第三个 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/