我有一个使用布局对象定义的CollectionViewController实例。
let layout = UICollectionViewFlowLayout()
(1)let friendsController = FriendsController(collectionViewLayout: layout)
window?.rootViewController = UINavigationController(rootViewController: friendsController)
所以在friendsController中我已经定义了字符串属性消息。
class FriendsController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
fileprivate let cellId = "cellId"
var messages: [Message]?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Recent"
collectionView?.backgroundColor = UIColor.white
collectionView?.register(MessageCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.alwaysBounceVertical = true
setupData()
}
......
是的,我可以从(1)friendsController获取消息。但我定义了一个FriendController类的实例,它定义了空的初始化程序,但我无法获取消息属性。我曾建议我启动UIViewColletionController类的父类,但是当看到有关FriendController类的其他信息(使用空初始化程序初始化)时,它向我显示它仍然是 class FriendsController:UICollectionViewController,UICollectionViewDelegateFlowLayout。如果有人可以提示,那会对我有很大的帮助
答案 0 :(得分:0)
1.解决方案:您可以在FriendsController
class FriendsController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
fileprivate let cellId = "cellId"
var messages: [Message]?
init(collectionViewLayout layout: UICollectionViewLayout, and messages: [String]) {
super.init(collectionViewLayout: layout)
self.messages = messages
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Recent"
collectionView?.backgroundColor = UIColor.white
collectionView?.register(MessageCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.alwaysBounceVertical = true
setupData()
}
然后
let messages: [Message] = []
let layout = UICollectionViewFlowLayout()
let friendsController = FriendsController(collectionViewLayout: layout, and: messages)
window?.rootViewController = UINavigationController(rootViewController: friendsController)
let messages: [Message] = []
let layout = UICollectionViewFlowLayout()
let friendsController = FriendsController(collectionViewLayout: layout, and: messages)
friendsController.messages = messages
window?.rootViewController = UINavigationController(rootViewController: friendsController)