Swift 3:将indexPath传递到另一个类

时间:2017-03-04 13:34:22

标签: swift swift3 uicollectionview uicollectionviewcell

我有一个collectionView,它返回一组帖子[Post]。在collectionViews单元格类(实际帖子)中,我有一个名为profileSegueContainer的视图。 isUserInteractionEnabled当然设置为true,我有一个UITapGestureRecognizer,在点击这个视图时运行一个函数。此函数只显示另一个ViewController。我希望能够在点击post.uid时将profileSegueContainer传递到此ViewController中。我该如何实现这一目标?谢谢。相关代码如下。任何建议都将受到高度赞赏......

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 PostCell: BaseCell {

    var homeController: HomeController?

    lazy var profileSegueContainer: UIView = {
        let view = UIView()
        view.isUserInteractionEnabled = true
        return view
    }()

    override func setupViews() {
        super.setupViews()

        let tap = UITapGestureRecognizer(target: self, action: #selector(handleProfileSegue))
        profileSegueContainer.addGestureRecognizer(tap)

       topBar.addSubview(profileSegueContainer)
    }

    func handleProfileSegue() {
        let userProfileController = UserProfileController()
        let navigationUserProfileController = UINavigationController(rootViewController: userProfileController)
        homeController?.present(navigationUserProfileController, animated: true, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:0)

希望我能正确理解你的问题。您想将值传递给新的UserProfileController对象吗?

向UserProfileController添加属性

class UserProfileController : UIViewController {
    public var postId : Int!
// more stuff ....
}

然后,您只需在创建对象后访问该属性:

func handleProfileSegue(sender: UITapGestureRecognizer) {
  let userProfileController = UserProfileController()
    userProfileController.postId = 2345
    // more stuff...
}

如果您需要,请告诉我。如果没有,我们会更新我们的帖子。