gpt4 book ai didi

swift 3 : Segue and pass in data from one class to another

转载 作者:行者123 更新时间:2023-11-30 12:36:31 25 4
gpt4 key购买 nike

我有一个名为 HomeController 的简单 UICollectionViewController,它有一堆单元格。这些单元格有一个自己的类,称为 PostCell。我还有一个 NSObject 类,它保存每个 Post 的所有数据(这些帖子是我的单元格)。在我的 PostCell 中,我有一个名为 profileSegueContainerUIView。当我点击这个 UIView 时,我想切换到一个新的 Controller 。我可以使用 PresentViewController 方法来实现此目的,但我也想传递该特定帖子的信息。例如,如果我点击一个 uid 为“1234”的单元格,我希望不仅能够连接到新 Controller ,而且还可以将此 uid 传递给它。当然,如果我点击下一个单元格并且该单元格的 post.uid 是“4567”,那么我也想在我继续时将其传递进去。我希望这是有道理的……我希望它的工作方式类似于 Instagram 的“点击用户即可访问他们的个人资料”功能。我希望这是有道理的。任何帮助将不胜感激,当然,我会标记为答案。谢谢。所有相关代码如下:

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)
}

最佳答案

Segue 在 View Controller 的上下文中具有非常具体的含义。您可能想使用短语“推送另一个 View Controller ”来代替。

首先,UINavigationController 通常只在场景中使用一次——它是跟踪 subview Controller 堆栈的 View Controller 。您只创建其中之一,并且您可能希望将 HomeController 设置为导航 Controller 的 rootViewController

然后,当用户点击 View 时,您应该将其转发到要添加到 HomeController 的方法;这将创建您的自定义 UserProfile 类,使用所需的数据对其进行配置,然后调用:navigationController?.pushViewController(userProfile, animating: true)

有许多优秀的教程向您展示这一切是如何工作的。

关于 swift 3 : Segue and pass in data from one class to another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42797551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com