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

标签 ios swift3 uicollectionview

预期结果

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

结果

失败

Cannot Call Value of Non-Function Type '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 的定义中您的参数controllerNameUICollectionViewController 类型这不是函数类型,因此您不能将其作为函数调用,即 controllerName(...) .

如果你更换

let viewController = controllerName(collectionViewLayout: layout)


let viewController = UserController(collectionViewLayout: layout)

从您的工作代码中,它编译得很好。此外,您不需要 controllerName参数了。反正我不太明白这是干什么用的。您是否希望能够指定 Controller 类型?然后你可以像 dan 建议的那样做(虽然这段代码需要 UICollectionViewController 因为你将使用 init(collectionViewLayout:) )。

您还可以像这样传递要嵌入到导航 Controller 中的 View Controller 的实例:
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 .这一切都取决于您想要达到的目标。

关于ios - 在 Swift 3 中无法调用非函数类型 'UICollectionView' 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42837473/

相关文章:

ios - 为什么最小项间间距不起作用?

ios - UITableViewCell 中的 UICollectionView 重用

swift - 填充不同类型数据的最佳且最有效的方法

ios - AQGridView EXC_BAD_ACCESS _basicHitTest :withEvent:

swift - 如何保存和显示带有核心数据的图像 - swift 3

ios - 滚动更多时间时 UiTableviewCell 重叠

Swift3 UISearchBar 无法正常工作

json - 几秒钟后快速刷新 JSON 数据

ios - 当应用程序在前台时更新来自推送通知的标签栏角标(Badge)计数

android - 单词上的点击事件 react native