Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now let's take a look at how to implement the iOS 6 pull to refresh feature. I'm going to create a working copy of our BWRSS-features-start, and I'm going to rename that as -02, and we'll open in Xcode by double clicking on the Xcode project file. We're going to be working in the ItemsTableViewController, and we notice up here at the top in our interface section, this is the private members or the instance variables for this class.
You'll notice there's a BOOL variable called canRefresh, and that gets set in -viewDidLoad. If I come down to -viewDidLoad, and we're going to come out to our Finder and load up this methods.txt file, and here is our new -viewDidLoad. I'm going to just copy that and come over here in to Xcode and I will paste that in place. So, you'll notice here I used the respondsToSelector method to check if our TableViewController response to the selector called refreshControl, and if it does, then I know that we can use Refresh so I can set this canRefresh equals true, or Yes in the case of Objective-C.
And I initialize our refreshControl with the UIRefreshControl and then I call addTarget on it to add the target, it is refreshInvoked:forState method, and it will be set for the control events, UIControlEventValueChanged. It looks a little bit complicated, and I guess it is, but this is just how you do it, and you'll pretty much always do it exactly this way. So, we're going to come back out here to our text editor, and I'm going to grab all of this support for reload gestures stuff here, and I'm going to come back into Xcode, and I'm going to paste it in right about here. Here is how this works.
Here's our refreshInvoked forState, and you'll remember that's the selector that we set as our target for the refreshControl event, right? So, when that gets called we reload our RSSFeed, and we send endRefreshing message to the RefreshControl, as simple as that. In order for that to happen, we have to be the first responder and so our class gets called with canBecomeFirstResponder to test whether or not we have that capability and so we use this canRefresh flag to return there.
When viewDidAppear, we're going to call becomeFirstResponder, and when view will disappear we resignFirstResponder. So that's all there is to it. When I save this and run it in the iPhone Simulator--I'm going to go ahead and add my test feed here so that's at ios.bw.org/testfeed, and I'll bring that up. There is five entries, and when I pull you see there is that little icon, and I pull it far enough, and it goes ahead it calls this rerefreshInvoked which reloads the feed and ends the refreshing, and there it is again.
So, we've now successfully added the pull to refresh feature to our app. This was easy for a number of reasons. First of all, Cocoa Touch Framework is really very well written and easy to code to then reloading the RSS Feed was also easy because our code is well-organized. If you do a good job of keeping your code clear and concise, then changing and modifying your code will always be a lot easier.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101355 Viewers
61 Video lessons · 88117 Viewers
71 Video lessons · 71955 Viewers
56 Video lessons · 103782 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.