亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在SWIFT中在視圖控制器和其他對象之間共享數據?

如何在SWIFT中在視圖控制器和其他對象之間共享數據?

尚方寶劍之說 2019-06-19 11:12:30
如何在SWIFT中在視圖控制器和其他對象之間共享數據?假設我在我的SWIFT應用程序中有多個視圖控制器,我希望能夠在它們之間傳遞數據。如果我在視圖控制器堆棧中有幾個層次,如何將數據傳遞給另一個視圖控制器?還是在選項卡欄視圖控制器中的選項卡之間?(注意,這個問題是“響鈴人”。)人們對它的要求太高了,所以我決定寫一篇關于這一主題的教程。見下面的答案。
查看完整描述

3 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

SWIFT 4

數據的快速傳輸有很多種方法。在這里,我要添加一些最好的方法。

1)使用故事板段

故事板分頁格對于在源視圖控制器和目標視圖控制器之間傳遞數據非常有用,反之亦然。

// If you want to pass data from ViewControllerB to ViewControllerA while user tap on back button of ViewControllerB.       
 @IBAction func unWindSeague (_ sender : UIStoryboardSegue) {
            if sender.source is ViewControllerB  {
                if let _ = sender.source as? ViewControllerB {
                    self.textLabel.text = "Came from B = B->A , B exited"
                }
            }
        }// If you want to send data from ViewControllerA to ViewControllerB       
         override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if  segue.destination is ViewControllerB {
                if let vc = segue.destination as? ViewControllerB {
                    vc.dataStr = "Comming from A View Controller"
                }
            }
        }

2)使用代表法

ViewControllerD

//Make the Delegate protocol in Child View Controller (Make the protocol in Class from You want to Send Data)    
protocol  SendDataFromDelegate {
        func sendData(data : String)
    }

    import UIKit

    class ViewControllerD: UIViewController {

        @IBOutlet weak var textLabelD: UILabel!

        var delegate : SendDataFromDelegate?  //Create Delegate Variable for Registering it to pass the data
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.            textLabelD.text = "Child View Controller"
        }

        @IBAction func btnDismissTapped (_ sender : UIButton) {
            textLabelD.text = "Data Sent Successfully to View Controller C using Delegate Approach"
            self.delegate?.sendData(data:textLabelD.text! )
            _ = self.dismiss(animated: true, completion:nil)
        }
    }

ViewControllerC

    import UIKit

    class ViewControllerC: UIViewController , SendDataFromDelegate {

        @IBOutlet weak var textLabelC: UILabel!

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.        }

        @IBAction func btnPushToViewControllerDTapped( _ sender : UIButton) {
            if let vcD = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerD") as?  ViewControllerD  {
                vcD.delegate = self // Registring Delegate (When View Conteoller D gets Dismiss It can call sendData method    
                //            vcD.textLabelD.text = "This is Data Passing by Referenceing View Controller D Text Label." 
                //Data Passing Between View Controllers using Data Passing                
                self.present(vcD, animated: true, completion: nil)
            }
        }

        //This Method will called when when viewcontrollerD will dismiss. (You can also say it is a implementation of Protocol Method) 
               func sendData(data: String) {
            self.textLabelC.text = data        }

    }


查看完整回答
反對 回復 2019-06-19
  • 3 回答
  • 0 關注
  • 808 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號