[英]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 . UINavigationController
的topViewController
方法返回一个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.