[英]Pass data from tableview to view controller in tab bar in Swift
[英]Data pass tableview cell to tab bar view controller in Swift
我是新手
的tableview包含多个图像工作良好的代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Media", for: indexPath)as! MediaCustomTableViewCell
let row = indexPath.row
let media = Mediainfo[row] as MediaEvent
cell.DisplayDate.text = media.date
cell.DisplayName.text = media.eventName
cell.selectionStyle = .none
cell.DisplayImage.downloadImageFrom(link:media.bannerImages, contentMode: UIViewContentMode.scaleAspectFit)
return cell
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let media = Mediainfo[(indexPath.row)] as MediaEvent
let ImageStoryboard = UIStoryboard(name: "Main", bundle: nil)
let ImageController = ImageStoryboard.instantiateViewController(withIdentifier: "IMAGESCOL")as!ImagesCollectionViewController
在这里,我将图像传递到集合视图,但是它不起作用:
ImageController.RecivedData1 = media.bannerImages
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabcontroller = storyboard.instantiateViewController(withIdentifier: "IMAGEVID") as! UITabBarController
navigationController?.pushViewController(tabcontroller, animated: true)
}
选择表格视图中的任何图像后,单元格图像应显示在带有集合视图的选项卡栏上。 我正在明智地显示图像和视频类别,可以使用标签栏控制器
数据传递到:
Tableview --->标签栏控制器===> 2个集合视图
如何使用标签栏控制器将tableview图像数据传递给集合?
在你的代码中
let ImageStoryboard = UIStoryboard(name: "Main", bundle: nil)
let ImageController = ImageStoryboard.instantiateViewController(withIdentifier: "IMAGESCOL") as! ImagesCollectionViewController
ImageController.RecivedData1 = media.bannerImages
这些代码不会执行任何操作,因为您声明了ImageController
,但没有使用/展示它。
因此,根据上下文,我假设您在Main.storyboard
有一个UITabBarController
,而UITabBarController
有两个UIViewController
,其中一个是ImageController
。 如果我是对的,则可以尝试以下代码:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "IMAGEVID") as! UITabBarController
// You should access the `imageController` through your `tabBarController`,
// not instantiate it from storyboard.
if let viewControllers = tabBarController.viewControllers,
let imageController = viewControllers.first as? ImageController {
imageController.recivedData1 = media.bannerImages
}
navigationController?.pushViewController(tabBarController, animated: true)
另一个建议是,您应该使用驼峰式大小写来命名属性。 有关更多详细信息,您可以参考此样式指南 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.