![](/img/trans.png)
[英]How to segue from UICollectionView Cell to another ViewController Swift
[英]Swift 4 - Push a ViewController from a UICollectionView Cell
我被困在我的項目中,需要一些幫助。 我想從包括我的UINavigationBar
的UICollectionViewCell
推送/呈現一個UICollectionViewController
。 我了解您實際上不能從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)
}
}
1-細胞內部
weak var delegate:UserProfileController?
2-內部cellForItemAt
cell.delegate = self
3-在細胞內使用
delegate?.present(newController, animated: true, completion: nil)
順便說一句,我認為你的意思是
delegate?.navigationController?.pushViewController(newController, animated: true)
如果要呈現新的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)
}
}
考慮到這樣做是不正確的。 使用MVC方式:移動
button.addTarget(self, action: #selector(followers), for: .touchUpInside)
方法中的UserProfileController
override 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)
}
我真的認為您根本不應該從單元格中進行演示,即使您引用了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無需擔心單元的內部工作原理,反之亦然。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.