【问题标题】:IOS Swift - Passing data between view controllers using container viewIOS Swift - 使用容器视图在视图控制器之间传递数据
【发布时间】:2015-12-28 01:52:34
【问题描述】:

我对如何正确使用容器视图有点困惑,我会尽力解释它。

我有一个具有动画功能的主视图控制器。

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 的一个实例并调用该函数,它似乎失去了它的“自我”相关性。

如果有人可以向我解释做这样的事情的最佳做法,那就太好了。

谢谢

【问题讨论】:

    标签: ios iphone swift segue


    【解决方案1】:

    根据您的解释,MainViewController 是ContainerViewController 的父级,因此要从ContainerViewController 访问closePicker,您可以这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      相关资源
      最近更新 更多