我陷入了自己的项目,需要一些帮助。我想从UICollectionViewController
的{strong>包括 UICollectionViewCell
推送/演示UINavigationBar
。我了解您实际上不能从Cell呈现ViewController吗?在这种情况下我该怎么办?我的问题是我的NavigationBar无法显示。
这是我的手机:
import UIKit
import Firebase
class UserProfileHeader: UICollectionViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
lazy var followersLabelButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("followers", for: .normal)
button.setTitleColor(UIColor.lightGray, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(followers), for: .touchUpInside)
return button
}()
@objc fileprivate func followers() {
let newController = NewController(collectionViewLayout: UICollectionViewFlowLayout())
self.window?.rootViewController?.present(newController, animated: true, completion: nil)
}
}
还有CollectionViewController:
import UIKit
import MapKit
import Firebase
class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate {
var userId: String?
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.white
collectionView?.register(UserProfileHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerId")
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerId", for: indexPath) as! UserProfileHeader
header.user = self.user
return header
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 200)
}
}
答案 0 :(得分:3)
1-牢房内部
weak var delegate:UserProfileController?
2-内部cellForItemAt
cell.delegate = self
3-在单元格中使用它
delegate?.present(newController, animated: true, completion: nil)
顺便说一句,我想你是说
delegate?.navigationController?.pushViewController(newController, animated: true)
答案 1 :(得分:0)
如果要显示新的ViewController,则应在Cell中创建一个新的委托,如下所示:
import UIKit
import Firebase
protocol CellDelegate {
func presentViewController()
}
class UserProfileHeader: UICollectionViewCell,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var delegate: CellDelegate?
lazy var followersLabelButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("followers", for: .normal)
button.setTitleColor(UIColor.lightGray, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(followers), for: .touchUpInside)
return button
}()
@objc func followers() {
delegate.presentViewController
}
}
在ViewController中,您应该使委托函数像这样
import UIKit
import MapKit
import Firebase
class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate
, CellDelegate {
var userId: String?
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.white
collectionView?.register(UserProfileHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerId")
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerId", for: indexPath) as! UserProfileHeader
header.user = self.user
// Remember to set the delegate for the cell
header.delegate = self
return header
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 200)
}
func presentViewController() {
let newController = NewController(collectionViewLayout: UICollectionViewFlowLayout())
self.window?.rootViewController?.present(newController, animated: true, completion: nil)
}
}
答案 2 :(得分:0)
这样做是不正确的。使用MVC方式: 移动
button.addTarget(self, action: #selector(followers), for: .touchUpInside)
到方法
中的UserProfileControlleroverride func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerId", for: indexPath) as! UserProfileHeader as! UserProfileHeader
header.followersLabelButton.addTarget(self, action: #selector(followers), for: .touchUpInside)
header.user = self.user
return header
}
并重写
@objc func followers() {
let newController = self.navigationController
newController?.present(newController, animated: true, completion: nil)
}
答案 3 :(得分:0)
我真的认为您根本不应该从单元格中进行演示,即使您引用了viewController。我宁愿在委托上调用一个函数,然后让它决定要做什么-
import UIKit
import Firebase
protocol UserProfileHeaderDelegate: class {
func followersTapped()
}
class UserProfileHeader: UICollectionViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
weak var headerDelegate: UserProfileHeaderDelegate?
lazy var followersLabelButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("followers", for: .normal)
button.setTitleColor(UIColor.lightGray, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(followers), for: .touchUpInside)
return button
}()
@objc fileprivate func followers() {
self.headerDelegate?.followersTapped()
}
}
在viewController中-
class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UserProfileHeaderDelegate {
// etc, etc......
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerId", for: indexPath) as! UserProfileHeader
header.user = self.user
// Remember to set the delegate for the cell
header.headerDelegate = self
return header
}
// Implement the UserProfileHeaderDelegate protocol - this is called from within the cell
// so all the cell knows is that its delegate implements this protocol, which has the followersTapped() function
func followersTapped() {
let newController = NewController(collectionViewLayout: UICollectionViewFlowLayout())
self.present(newController, animated: true, completion: nil)
}
}
这样,您的viewController不必担心单元的内部工作原理,反之亦然。