我有一个名为HomeController
的简单UICollectionViewController,它有一堆单元格。这些单元格有一个自己的类PostCell
。我还有一个NSObject类,它包含每个Post
的所有数据(这些帖子是我的单元格)。在我的PostCell中,我有UIView
名为profileSegueContainer
。当我点击这个UIView时,我想要转向一个新的控制器。我能够使用presentViewController方法实现这一点,但我也想传递该特定帖子的信息。例如,如果我点击一个uid
为“1234”的单元格,我希望不仅可以转到新控制器,还可以将此uid传递给它。当然,如果我点击下一个单元格并且该单元格的post.uid
是“4567”,那么我想在我离开时传递它。我希望这是有道理的......我希望它的工作类似于Instagrams“点击用户来获取他们的个人资料”功能。我希望这是有道理的。任何帮助将受到高度赞赏,当然,我会标记为答案。谢谢。所有相关代码如下:
class Post: NSObject {
var author: String?
var avatar_image_url: String?
var likes: Int?
var postImageUrl: String?
var uid: String?
var post_id: String?
var hashtags: [String]?
}
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var postCell: PostCell?
var userProfileController: UserProfileController?
var posts = [Post]()
var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView?.backgroundColor = grayBackgroundColor
self.collectionView?.register(PostCell.self, forCellWithReuseIdentifier: reuseIdentifier)
self.collectionView?.contentInset = UIEdgeInsetsMake(16, 0, 16, 0)
self.collectionView?.alwaysBounceVertical = true
self.collectionView?.showsVerticalScrollIndicator = false
setupNavigationBarBranding()
checkIfUserIsLoggedIn()
fetchPosts()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let frameWidth = self.view.frame.size.width
let width = frameWidth - (16 + 16)
let height = frameWidth - (16 / 9) + 50 + 50
return CGSize(width: width, height: height)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
cell.homeController = self
cell.post = posts[indexPath.item]
userProfileController?.post = posts[indexPath.item]
return cell
}
}
class PostCell: BaseCell {
var homeController: HomeController?
var userProfileController: UserProfileController?
var post: Post? {
didSet {
if let postDisplayName = post?.author {
displayName.text = postDisplayName
}
if let postImageUrl = post?.postImageUrl {
postImage.loadImageUsingCacheWithUrlString(urlString: postImageUrl)
}
if let postProfileImage = post?.avatar_image_url {
profileImage.loadImageUsingCacheWithUrlString(urlString: postProfileImage)
}
}
}
lazy var profileSegueContainer: UIView = {
let view = UIView()
view.isUserInteractionEnabled = true
return view
}()
func handleProfileSegue() {
let userProfile = UserProfileController()
let navigationUserProfile = UINavigationController(rootViewController: userProfile)
homeController?.present(navigationUserProfile, animated: true, completion: nil)
}
override func setupViews() {
super.setupViews()
let tap = UITapGestureRecognizer(target: self, action: #selector(handleProfileSegue))
profileSegueContainer.addGestureRecognizer(tap)
addSubview(profileSegueContainer)
_ = profileSegueContainer.anchor(profileImage.topAnchor, left: profileImage.leftAnchor, bottom: profileImage.bottomAnchor, right: displayName.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
}
答案 0 :(得分:0)
Segue在View Controllers的上下文中具有非常特殊的含义。您可能希望使用短语"推送另一个视图控制器"代替。
首先,UINavigationController
通常仅在场景中使用一次 - 它是跟踪一组子视图控制器的视图控制器。您只需创建其中一个,并且可能希望将HomeController
设置为导航控制器rootViewController
。
然后,当用户点击视图时,您应将其转发到您将添加到HomeController的方法;这将创建您的自定义UserProfile
类,使用所需的数据对其进行配置,然后调用:navigationController?.pushViewController(userProfile, animating: true)
有很多很好的教程可以告诉你这一切是如何运作的。