第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在SWIFT中在視圖控制器和其他對(duì)象之間共享數(shù)據(jù)?

如何在SWIFT中在視圖控制器和其他對(duì)象之間共享數(shù)據(jù)?

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

3 回答

?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

SWIFT 4

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

1)使用故事板段

故事板分頁格對(duì)于在源視圖控制器和目標(biāo)視圖控制器之間傳遞數(shù)據(jù)非常有用,反之亦然。

// 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        }

    }


查看完整回答
反對(duì) 回復(fù) 2019-06-19
  • 3 回答
  • 0 關(guān)注
  • 804 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)