簡體   English   中英

iOS Swift-使用容器視圖在視圖控制器之間傳遞數據

[英]IOS Swift - Passing data between view controllers using container view

對於如何正確使用容器視圖,我有些困惑,我將盡我所能解釋。

我有一個具有動畫功能的主視圖控制器。

import UIKit

class MainViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

// Run view setups
override func viewDidLoad() {
    super.viewDidLoad()
}

func closePicker(){
    self.view.layoutIfNeeded();
    UIView.animateWithDuration(0.5, animations: {
            self.countryPickerConst.constant = -206;
        self.view.layoutIfNeeded();
    })
} 

}

在界面構建器中,我添加了帶有新視圖控制器的容器視圖,其中包含如下所示的按鈕:

import UIKit

class ContainerViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

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



@IBAction func runAnimation(sender: UIButton) {

    //I want to call the function in my other view controller

}


}

在動作runAnimation中,我想在MainViewController中調用該函數。 如果我只是創建MainViewController的實例並調用該函數,則它似乎失去了“自我”的相關性。

如果有人可以向我解釋這樣做的最佳實踐,那將是很好的。

謝謝

根據您的解釋, MainViewControllerContainerViewController的父級,因此您可以從ContainerViewController訪問closePicker

@IBAction func runAnimation(sender: UIButton) {
  (self.parentViewController as! MainViewController).closePicker()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM