2017-03-04 81 views
0

我有一個collectionView,它返回一個帖子數組,[Post]。在collectionViews單元類(實際的帖子)裏面,我有一個叫profileSegueContainer的視圖。 isUserInteractionEnabled當然設置爲true,並且我有一個UITapGestureRecognizer,它在點擊此視圖時運行一個函數。這個函數只提供了另一個ViewController。當我點擊profileSegueContainer時,我希望能夠將post.uid傳入此ViewController。我如何實現這一目標?謝謝。相關代碼如下。任何意見將不勝感激...Swift 3:將indexPath傳入另一個類

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

可以achive使用自定義點擊類,試試這個代碼 進口的UIKit 類customTap:UITapGestureRecognizer {VAR objPost:帖子! =零 } // 使用像: 讓自來水= customTap(目標:自我,動作:#selector(handleProfileSegue :)) tap.objPost = yourPostDataobject profileSegueContainer.addGestureRecognizer(TAP) –

+0

@Jay Mahta感謝的人。意義重大。你能舉個例子嗎? – Jimmy

+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... 
} 

讓我知道這是你需要的東西。如果沒有,我們會更新我們的帖子。

+0

不幸的是,當我嘗試在UserProfileController加載後打印postId時,它將返回一個零 – Jimmy

+0

@Jimmy是否可以在'setupViews()'方法中打印'postId'? –

相關問題