From d0fec996ff203358bac073e7a5a6d2de6d86edb7 Mon Sep 17 00:00:00 2001 From: Robert Panzer Date: Fri, 14 Sep 2012 18:15:51 +0200 Subject: [PATCH] Use UIRefreshControl on iOS6 --- .../PullToRefreshTableViewController.m | 54 +++++++++++++++---- Hub To Go.xcodeproj/project.pbxproj | 4 ++ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Github To Go/PullToRefreshTableViewController.m b/Github To Go/PullToRefreshTableViewController.m index fc3bae5..71f2201 100644 --- a/Github To Go/PullToRefreshTableViewController.m +++ b/Github To Go/PullToRefreshTableViewController.m @@ -7,7 +7,7 @@ // #import "PullToRefreshTableViewController.h" - +#import @implementation ReloadLabel @@ -65,6 +65,7 @@ -(void)stopActivity { @interface PullToRefreshTableViewController () +@property(nonatomic) BOOL useRefreshControl; @end @implementation PullToRefreshTableViewController @@ -73,18 +74,43 @@ @implementation PullToRefreshTableViewController @synthesize reloadPossible; @synthesize isReloading; --(void)viewWillAppear:(BOOL)animated { - if (self.reloadLabel == nil) { +@class UIRefreshControl; + +-(void) viewDidLoad { + [super viewDidLoad]; + Class refreshControlClass = NSClassFromString(@"UIRefreshControl"); + + if (refreshControlClass != NULL) { + self.useRefreshControl = YES; + // New iOS 6 Refresh Control + id refreshControl = [[refreshControlClass alloc] init]; + //TODO: Remove id cast once iOS6 is minimum requirement + [self setRefreshControl:refreshControl]; + NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys: + [UIColor blackColor], kCTForegroundColorAttributeName, + nil]; + + NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"Pull to refresh" attributes:dict]; + + [refreshControl setAttributedTitle:title]; + [refreshControl addTarget:self action:@selector(reload:) forControlEvents:UIControlEventValueChanged]; + } else { self.reloadLabel = [[ReloadLabel alloc] initWithFrame:CGRectMake(0.0f, -40.0f, self.tableView.frame.size.width, 40.0f)]; [self.tableView addSubview:self.reloadLabel]; - } - [self.tableView.panGestureRecognizer addTarget:self action:@selector(swiped:)]; +} + +-(void)viewWillAppear:(BOOL)animated { + if (!self.useRefreshControl) { + [self.tableView.panGestureRecognizer addTarget:self action:@selector(swiped:)]; + } } -(void)viewWillDisappear:(BOOL)animated { - [self.tableView.panGestureRecognizer removeTarget:self action:@selector(swiped:)]; + if (!self.useRefreshControl){ + [self.tableView.panGestureRecognizer removeTarget:self action:@selector(swiped:)]; + } } -(void) swiped:(UIPanGestureRecognizer*)recognizer { @@ -120,6 +146,10 @@ -(void) swiped:(UIPanGestureRecognizer*)recognizer { } } +-(void)reload:(id)sender { + [self reload]; +} + -(void)reload {} -(void)willReload { @@ -128,11 +158,15 @@ -(void)willReload { } -(void)reloadDidFinish { - if (self.isReloading) { - [self.tableView setContentOffset:CGPointMake(0.0f, 0.0f) animated:YES]; + if (self.useRefreshControl) { + [self.refreshControl endRefreshing]; + } else { + if (self.isReloading) { + [self.tableView setContentOffset:CGPointMake(0.0f, 0.0f) animated:YES]; + } + self.isReloading = NO; + [self.reloadLabel stopActivity]; } - self.isReloading = NO; - [self.reloadLabel stopActivity]; } @end diff --git a/Hub To Go.xcodeproj/project.pbxproj b/Hub To Go.xcodeproj/project.pbxproj index 307cba1..e23c6be 100644 --- a/Hub To Go.xcodeproj/project.pbxproj +++ b/Hub To Go.xcodeproj/project.pbxproj @@ -81,6 +81,7 @@ 94C6566D15025FAD00E85813 /* UITableViewCell+GithubEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = 94C6566C15025FAD00E85813 /* UITableViewCell+GithubEvent.m */; }; 94C656711503727D00E85813 /* UITableViewCell+Person.m in Sources */ = {isa = PBXBuildFile; fileRef = 94C656701503727D00E85813 /* UITableViewCell+Person.m */; }; 94C8AE74160385900026FD9E /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 94C8AE73160385900026FD9E /* Default-568h@2x.png */; }; + 94C8AE7616038C540026FD9E /* CoreText.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 94C8AE7516038C540026FD9E /* CoreText.framework */; }; 94CC3C3B1523B31B00F4B25B /* PersonListTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 94CC3C391523B31B00F4B25B /* PersonListTableViewController.m */; }; 94CC3C3C1523B31B00F4B25B /* PersonListTableViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 94CC3C3A1523B31B00F4B25B /* PersonListTableViewController.xib */; }; 94CCEDF815951AF700D5D810 /* EventRootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 94CCEDF715951AF700D5D810 /* EventRootViewController.m */; }; @@ -229,6 +230,7 @@ 94C6566F1503727D00E85813 /* UITableViewCell+Person.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITableViewCell+Person.h"; sourceTree = ""; }; 94C656701503727D00E85813 /* UITableViewCell+Person.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UITableViewCell+Person.m"; sourceTree = ""; }; 94C8AE73160385900026FD9E /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; + 94C8AE7516038C540026FD9E /* CoreText.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreText.framework; path = System/Library/Frameworks/CoreText.framework; sourceTree = SDKROOT; }; 94CC3C381523B31B00F4B25B /* PersonListTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PersonListTableViewController.h; sourceTree = ""; }; 94CC3C391523B31B00F4B25B /* PersonListTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PersonListTableViewController.m; sourceTree = ""; }; 94CC3C3A1523B31B00F4B25B /* PersonListTableViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = PersonListTableViewController.xib; sourceTree = ""; }; @@ -266,6 +268,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 94C8AE7616038C540026FD9E /* CoreText.framework in Frameworks */, 940AF862159BAD7200F8A5EB /* MessageUI.framework in Frameworks */, 940AF85F159B7A8A00F8A5EB /* Twitter.framework in Frameworks */, 94C2F9FC158CBEDF0006640F /* QuartzCore.framework in Frameworks */, @@ -426,6 +429,7 @@ 949FB12414B3327700D3E713 = { isa = PBXGroup; children = ( + 94C8AE7516038C540026FD9E /* CoreText.framework */, 940AF861159BAD7200F8A5EB /* MessageUI.framework */, 940AF85E159B7A8A00F8A5EB /* Twitter.framework */, 94C2F9FB158CBEDF0006640F /* QuartzCore.framework */,