2017-03-14 11 views
0

私は単純なUICollectionViewControllerというHomeControllerというセルを持っています。これらのセルにはPostCellという独自のクラスがあります。私はすべてPost(これらの投稿は私の細胞です)のすべてのデータを保持するNSObjectクラスも持っています。私のポストセルの中には、profileSegueContainerと呼ばれるUIViewがあります。私はこのUIViewをタップすると、私は新しいコントローラにsegueしたい。私はpresentViewControllerメソッドでこれを達成することができますが、私はその特定の投稿の情報を渡したいと思います。たとえば、uidが "1234"のセルをタップすると、新しいコントローラにセグを送るだけでなく、このuidを渡すこともできます。もちろん、次のセルをタップして、そのセルのpost.uidが「4567」であれば、私はそれを渡したいと思います。私はこれが意味をなさないことを願っています...私はそれがInstagrams "ユーザのプロファイルに到達するためにタップ"のような機能をしたい。私はこれが理にかなってほしい。どんな助力も高く評価され、もちろん私は答えとしてマークします。ありがとうございました。関連するすべてのコードは次のとおりです。スウィフト3:あるクラスから別のクラスにデータを転送して渡す

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

[prepareForSegue(_:)](https://developer.apple.com/reference/uikit/uiviewcontroller/1621490-prepareforsegue) – joeybladb

+0

@joeybladbこれを説明してもらえますか?私はストーリーボードを使用していませんもbtw ... – Jimmy

答えて

0

SegueはView Controllerのコンテキストで非常に具体的な意味を持ちます。代わりに、代わりに "別のビューコントローラをプッシュ"というフレーズを使用したいと思うかもしれません。

まず、UINavigationControllerはシーン内で1回だけ使用されます。サブビューコントローラのスタックを追跡するビューコントローラです。そのうちの1つだけを作成し、ナビコントローラーのrootViewControllerとしてHomeControllerを設定したいとします。

次に、ユーザーがビューをタップすると、これをHomeControllerに追加するメソッドに転送する必要があります。カスタムUserProfileクラスが作成され、必要なデータを設定してから、次のメソッドを呼び出します。navigationController?.pushViewController(userProfile, animating: true)

このようなことを示すチュートリアルがたくさんあります。

関連する問題