简体   繁体   English

如何将数据传递给嵌入在导航控制器中的集合视图控制器

[英]How to pass data to collection view controller embedded in navigation controller

I want to pass integer value from MainViewController to CollectionViewController embedded in UINavigationViewController . 我想将MainViewController整数值传递给UINavigationViewController嵌入的CollectionViewController The code is written in MainViewController.m file within prepareForSegue method. 代码在prepareForSegue方法中的MainViewController.m文件中编写。 I am getting warning as: 我收到警告:

Incompatible pointer types initializing ' CollectionViewController *' with an expression of type ' UIViewController * _Nullable' 不兼容的指针类型使用类型' UIViewController * _Nullable'的表达式初始化' CollectionViewController *'

The warning is for the code: 警告是针对代码的:

UINavigationController *navController = (UINavigationController*)[segue destinationViewController];

CollectionViewController *collectionVC = [navController topViewController];

For passing integer I am using: 对于传递整数我正在使用:

[collectionVC setReceivedCategoryId:categoryIdToSend];

UINavigationController 's topViewController method returns an UIViewController object as shown is the Apple documentation . UINavigationControllertopViewController方法返回一个UIViewController对象,如Apple文档所示。 Cast the object and it will work. 投射对象,它将工作。

 UINavigationController *navController = (UINavigationController*)[segue destinationViewController];

// Add the explicit cast
CollectionViewController *collectionVC = (CollectionViewController*)[navController topViewController];

[navController topViewController]; returns a UIViewController object. 返回一个UIViewController对象。 But we know that it is a CollectionViewController . 但我们知道它是一个CollectionViewController So we add a typecast, like so : 所以我们添加一个类型转换,如下所示:

CollectionViewController *collectionVC =(CollectionViewController*) [navController topViewController];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从一个视图控制器传递到导航控制器中嵌入的另一个视图控制器 - How to pass data from one view controller to another view controller which is embedded in navigation controller 如何将数据对象传递给嵌入在不同故事板中的导航控制器中的视图控制器 - How to Pass Data Object to View Controller Embedded in Navigation Controller in Different Storyboard 将数据传递到导航控制器中的另一个视图控制器 - Pass data to another view controller in navigation controller 将变量传递给Objective-C中嵌入在导航控制器中的View Controller - Pass variable to View Controller Embedded in Navigation Controller in Objective-C 如何将数据数据从集合视图单元传递到视图控制器 - how to pass data data from collection view cell to view controller 如何将变量传递给嵌入在导航 Controller 中的 ViewController? - How to pass variable to a ViewController that is embedded in a Navigation Controller? 视图控制器中的集合视图,单元格触摸集合视图本身的顶部边框(嵌入在导航控制器中) - Collection View in View Controller, Cell touching the top border of Collection View itself (embedded in navigation controller) 如何将数据从视图控制器传递到导航控制器,然后传递到另一个视图控制器? - How to pass data from view controller to a navigation controller and then to another view controller? 将数据传递到嵌入在Container View Controller中的View Controller - Pass data to View Controller embedded inside a Container View Controller 如何以模态方式呈现嵌入导航控制器中的视图控制器? - How to present view controller embedded in navigation controller modally?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM