繁体   English   中英

在Swift 3中无法调用非函数类型'UICollectionView'的值

[英]Cannot Call Value of Non-Function Type 'UICollectionView' in Swift 3

预期结果

在自定义标签栏控制器中创建标签栏图标的通用私有方法。 应该为每个指示的控制器成功绘制标签栏图标。

结果

与失败

无法调用非功能类型'UICollectionView'的值

失败(通用)代码:

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


       }

工作(非通用)代码

 let userLayout = UICollectionViewFlowLayout()
let userController = UserController(collectionViewLayout: userLayout)
let navController = UINavigationController(rootViewController: userController)
navController.tabBarItem.image = UIImage(named: "loc-map-route")

有关

class UserController : UICollectionViewController, UICollectionViewDelegateFlowLayout
{
    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = UIColor.green

    }
}

环境

iOS 10 Swift 3 Xcode 8

谢谢。

在您的createNavController定义中,参数controllerName的类型为UICollectionViewController ,并且不是函数类型,因此无法将其作为函数调用,即controllerName(...)

如果您更换

let viewController = controllerName(collectionViewLayout: layout)

let viewController = UserController(collectionViewLayout: layout)

从您的工作代码,它可以正常编译。 另外,您不再需要controllerName参数。 我还是不太明白这是什么意思。 您是否希望能够指定控制器类型 然后,您可以像dan建议一样执行此操作(尽管此代码需要UICollectionViewController因为您将使用init(collectionViewLayout:) )。

您也可以像这样传递要嵌入到导航控制器中的视图控制器的实例:

private func createNavController(imageName: String, rootViewController: UIViewController) -> UINavigationController
{
           let navController = UINavigationController(rootViewController: rootViewController)
           navController.tabBarItem.image = UIImage(named: imageName)
           return navController
}

并这样称呼它:

let navController = createNavController(
    imageName: "loc-map-route",
    rootViewController: UserController(collectionViewLayout: UICollectionViewFlowLayout()))

实际上,这将不需要UICollectionViewController因为UINavigationController.init(rootViewController:)需要一个UIViewController 这完全取决于您要实现的目标。

你想要这样的东西吗?

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
}

然后,您可以执行以下操作: let nav = createNavController(imageName: "loc-map-route", UserCollectionController.self)并且nav将以UserCollectionController作为其根。

我认为controllerName是UICollectionViewController的实例,因此它是一个已初始化的UICollectionViewController。 它不是UICollectionViewController子类的名称。 因此,如果您只想设置该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
}

您会这​​样称呼它:

let navController = createNavController(imageName: "my_image", myCreatedUserController)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM