[英]Cannot Call Value of Non-Function Type 'UICollectionView' in Swift 3
Intended Outcome 预期结果
A generic private method for creating tabbbar icons in a custom tabbar controller. 在自定义标签栏控制器中创建标签栏图标的通用私有方法。 Should successfully paint the tabbar icons for each indicated controller.
应该为每个指示的控制器成功绘制标签栏图标。
Result 结果
Fails with 与失败
Cannot Call Value of Non-Function Type 'UICollectionView'
无法调用非功能类型'UICollectionView'的值
Failing (Generic) Code: 失败(通用)代码:
private func createNavController(imageName: String, _ controllerName: UICollectionViewController) -> UINavigationController {
let layout = UICollectionViewFlowLayout()
Thrown Here-> let viewController = controllerName(collectionViewLayout: layout)
let navController = UINavigationController(rootViewController: viewController)
navController.tabBarItem.image = UIImage(named: imageName)
return navController
}
Working (non-generic) Code 工作(非通用)代码
let userLayout = UICollectionViewFlowLayout()
let userController = UserController(collectionViewLayout: userLayout)
let navController = UINavigationController(rootViewController: userController)
navController.tabBarItem.image = UIImage(named: "loc-map-route")
Related 有关
class UserController : UICollectionViewController, UICollectionViewDelegateFlowLayout
{
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.green
}
}
Environment 环境
ios 10 swift 3 xcode 8
iOS 10 Swift 3 Xcode 8
Thanks. 谢谢。
In your definition of createNavController
your parameter controllerName
is of type UICollectionViewController
and that is not a function type so you cannot call it as a function, ie controllerName(...)
. 在您的
createNavController
定义中,参数controllerName
的类型为UICollectionViewController
,并且不是函数类型,因此无法将其作为函数调用,即controllerName(...)
。
If you replace 如果您更换
let viewController = controllerName(collectionViewLayout: layout)
with 同
let viewController = UserController(collectionViewLayout: layout)
from your working code, it compiles fine. 从您的工作代码,它可以正常编译。 Also, you don't need the
controllerName
parameter anymore. 另外,您不再需要
controllerName
参数。 I don't quite understand what this is for anyway. 我还是不太明白这是什么意思。 Did you want to be able to specify the controller type ?
您是否希望能够指定控制器类型 ? Then you could do it like dan suggests (although this code requires a
UICollectionViewController
because you would be using init(collectionViewLayout:)
). 然后,您可以像dan建议一样执行此操作(尽管此代码需要
UICollectionViewController
因为您将使用init(collectionViewLayout:)
)。
You could also pass an instance of the view controller you want embedded in the navigation controller like this: 您也可以像这样传递要嵌入到导航控制器中的视图控制器的实例:
private func createNavController(imageName: String, rootViewController: UIViewController) -> UINavigationController
{
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.image = UIImage(named: imageName)
return navController
}
and call it like this: 并这样称呼它:
let navController = createNavController(
imageName: "loc-map-route",
rootViewController: UserController(collectionViewLayout: UICollectionViewFlowLayout()))
This would indeed not require a UICollectionViewController
because UINavigationController.init(rootViewController:)
takes a UIViewController
. 实际上,这将不需要
UICollectionViewController
因为UINavigationController.init(rootViewController:)
需要一个UIViewController
。 It all depends on what you want to achieve. 这完全取决于您要实现的目标。
Do you want something like this? 你想要这样的东西吗?
private func createNavController<T: UICollectionViewController>(imageName: String, _ controllerName: T.Type) -> UINavigationController {
let layout = UICollectionViewFlowLayout()
let viewController = controllerName.init(collectionViewLayout: layout)
let navController = UINavigationController(rootViewController: viewController)
navController.tabBarItem.image = UIImage(named: imageName)
return navController
}
Then you can do: let nav = createNavController(imageName: "loc-map-route", UserCollectionController.self)
and nav will have a UserCollectionController
as its root. 然后,您可以执行以下操作:
let nav = createNavController(imageName: "loc-map-route", UserCollectionController.self)
并且nav将以UserCollectionController
作为其根。
I think that controllerName is an instance of UICollectionViewController, so it is an already initialized UICollectionViewController. 我认为controllerName是UICollectionViewController的实例,因此它是一个已初始化的UICollectionViewController。 It is not the name of the UICollectionViewController subclass.
它不是UICollectionViewController子类的名称。 So if you just want to set the collectionViewLayout of that UICollectionViewController, I'd do:
因此,如果您只想设置该UICollectionViewController的collectionViewLayout,则可以这样做:
private func createNavController(imageName: String, _ controllerName: UICollectionViewController) -> UINavigationController
{
let layout = UICollectionViewFlowLayout()
let viewController = controllerName.collectionViewLayout = layout
let navController = UINavigationController(rootViewController: viewController)
navController.tabBarItem.image = UIImage(named: imageName)
return navController
}
The you would call it like this: 您会这样称呼它:
let navController = createNavController(imageName: "my_image", myCreatedUserController)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.