[英]How to pass data to table view controller being instantiated from UIStoryboard?
I have a ViewController with an UIScrollView which contains two TableViewControllers. 我有一个带有UIScrollView的ViewController,其中包含两个TableViewControllers。
let allViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController;
let favoritesViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController;
At some point inside the scrollViewDidEndDecelerating delegate I want to send the current page from UIScrollView (1 or 2) to my ContentViewController: 在scrollViewDidEndDecelerating委托内的某个时刻,我想将当前页面从UIScrollView(1或2)发送到我的ContentViewController:
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let page = Int(scrollView.contentOffset.x / scrollView.frame.size.width)
// how to send page to ContentViewController
//delegate.pageChanged(page)
}
If I don't use prepareForSegue, how to actually send the data to may UITableViewController ? 如果我不使用prepareForSegue,如何实际将数据发送到UITableViewController?
What I tried is add a protocol to ViewController, but I can't initialize the delegate inside ContentVieController: 我试过的是向ViewController添加协议,但是无法在ContentVieController内初始化委托:
protocol PageChangedDelegate {
func pageChanged(page: Int)
}
var delegate: PageChangedDelegate!
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let page = Int(scrollView.contentOffset.x / scrollView.frame.size.width)
delegate.pageChanged(page) // delegate is nil here
}
when you instantiate a viewController and you are down casting use "as!" 当您实例化viewController并进行强制转换时,请使用“ as!”。 you now have access to the variables of that view controller, 您现在可以访问该视图控制器的变量,
lets assume your content view controller has variables A,B,C you can do it like this 假设您的内容视图控制器具有变量A,B,C,您可以像这样进行操作
let allViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController
allViewController.A = "Setting variable A on view controller"
allViewController.B = "hi"
allViewController.C = "the view controller now has its variables assigned"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.