How to Use the TweetViewDelegate Protocol

ios
fabric
swift
twitterkit

#1

Hi! I’m new to developing with Twitter and iOS. I’ve been trying for some time to figure out how to use the TWTRTweetViewDelegate protocol with a TWTRTimelineViewController. Here is a minimal example that I would expect to work, but unfortunately does not. Any clues? I think I’m close.

class ViewController: UIViewController, TWTRTweetViewDelegate {

// Define the delegate
weak var delegate: TWTRTweetViewDelegate?

// Function implementing one of the delegate protocols (DOES NOT WORK!)
func tweetView(tweetView: TWTRTweetView!, didSelectTweet tweet: TWTRTweet!){
    // If the delegate protocol works, we would print the following message
    print("Worked!")
}
override func viewDidLoad() {
    super.viewDidLoad()

    // Add a button to the center of the view to show the timeline (not related to the delegate)
    let button = UIButton(type: .System)
    button.setTitle("Get User Data", forState: .Normal)
    button.sizeToFit()
    button.center = view.center
    button.addTarget(self, action: #selector(showTimeline), forControlEvents: [.TouchUpInside])
    view.addSubview(button)
}

func showTimeline() {
    // Create an API client and data source to fetch Tweets for the timeline
    let client = TWTRAPIClient()
    // Create the datasource
    let dataSource = TWTRCollectionTimelineDataSource(collectionID: "576828964162965504", APIClient: client)
    // Create the timeline view controller
    let timelineViewController = TWTRTimelineViewController(dataSource: dataSource)
    // Assign the delegate to the view controller
    timelineViewController.tweetViewDelegate = delegate
    ...

}

Yet, tweetView:didSelectTweet does not get called when tapping on a tweet in the collection. Any help would be appreciated.


#2

I had a feeling asking the question would help me figure out the answer:

Using the same code as above, change
// Assign the delegate to the view controller timelineViewController.tweetViewDelegate = delegate
to
// Assign the delegate to the view controller timelineViewController.tweetViewDelegate = self

which means you can also REMOVE this line as a variable declaration:
// Define the delegate weak var delegate: TWTRTweetViewDelegate?

Thanks, and sorry for the noise!
Mike


#3