作業中のアプリの問題を修正しようとしています。ここで質問を検索して閲覧した後、デリゲートといくつかの方法に問題があると思います。
このコードは、UIScrollView内に埋め込まれたUIViewを使用して「プルダウンして更新」を実装するためのものです。
私が実装しているクラスがあります:
RefreshView.h:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
typedef enum{
PullRefreshPulling = 0,
PullRefreshNormal,
PullRefreshLoading,
} PullRefreshState;
@protocol RefreshViewDelegate;
@interface RefreshView : UIScrollView <UIScrollViewDelegate> {
id __unsafe_unretained refreshDelegate;
PullRefreshState state;
UILabel *lastUpdatedLabel;
UILabel *statusLabel;
CALayer *arrowImage;
UIActivityIndicatorView *activityView;
}
@property(nonatomic,unsafe_unretained) id <RefreshViewDelegate> refreshDelegate;
- (void)viewDidScroll:(UIScrollView *)scrollView;
- (void)viewDidEndDragging:(UIScrollView *)scrollView;
- (void)viewDataDidFinishedLoading:(UIScrollView *)scrollView;
@end
@protocol RefreshViewDelegate
- (void)didTriggerRefresh:(RefreshView*)view;
- (BOOL)dataIsLoading:(RefreshView*)view;
@end
RefreshView.m:
#import "RefreshView.h"
@implementation RefreshView
- (void)viewDidEndDragging:(UIScrollView *)scrollView {
NSLog(@"RefreshView - viewDidEndDragging");
BOOL loading = NO;
if ([refreshDelegate respondsToSelector:@selector(dataIsLoading:)]) { // responds with scrollView.delegate ? ? ?
NSLog(@"RefreshView - respondsToSelector:@selector(dataIsLoading:)");
loading = [refreshDelegate dataIsLoading:self];
}
if (scrollView.contentOffset.y <= - 65.0f && !loading) {
NSLog(@"RefreshView - dataIsNotLoading");
if ([refreshDelegate respondsToSelector:@selector(didTriggerRefresh:)]) { // works with scrollView.delegate ? ? ?
NSLog(@"RefreshView - respondsToSelector:@selector(didTriggerRefresh:)");
[refreshDelegate didTriggerRefresh:self];
}
[self setState:PullRefreshLoading];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
scrollView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
[UIView commitAnimations];
}
}
SearchViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "RefreshView.h"
@interface SearchViewController : UIViewController <MKMapViewDelegate, RefreshViewDelegate, UIScrollViewDelegate> {
RefreshView *refreshView;
BOOL reloading;
}
@property (nonatomic, strong) UIScrollView *mainView;
@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) NSArray *info;
@property (nonatomic,strong) UINavigationBar *navBar;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (strong, nonatomic) NSTimer *locationTimer;
@property (nonatomic, strong) UIButton *currentLocationButton;
@property (nonatomic, strong) UIButton *locationServicesButton;
- (void)reloadData;
- (void)doneLoadingData;
@end
SearchViewControler.m
#import <QuartzCore/QuartzCore.h>
#import <CoreLocation/CoreLocation.h>
#import "SearchViewController.h"
#import "ProfileViewController.h"
#import "RefreshView.h"
#import "AppDelegate.h"
@interface SearchViewController ()
@end
@implementation SearchViewController
@synthesize mainView;
- (void)viewDidLoad {
[super viewDidLoad];
.
.
.
// Main view
mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
mainView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * 2);
mainView.delegate = self;
[self.view addSubview:mainView];
.
.
.
if (refreshView == nil) {
RefreshView *pullToRefreshView = [[RefreshView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.mainView.bounds.size.height, self.view.frame.size.width, self.mainView.bounds.size.height)];
pullToRefreshView.refreshDelegate = self;
[self.mainView addSubview:pullToRefreshView];
refreshView = pullToRefreshView;
}
.
.
.
}
- (void)viewDidUnload {...}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {...}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[refreshView viewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"SearchViewController - scrollViewDidEndDragging");
[refreshView viewDidEndDragging:scrollView];
}
- (void)didTriggerRefresh:(RefreshView*)view{
NSLog(@"SearchViewController - didTriggerRefresh");
[self reloadData];
[self performSelector:@selector(doneLoadingData) withObject:nil afterDelay:0.5f];
}
- (BOOL)dataIsLoading:(RefreshView*)view{
NSLog(@"SearchViewController - dataIsLoading");
[NSThread detachNewThreadSelector:@selector(displayMap) toTarget:self withObject:nil];
return reloading;
}
- (void)reloadData{
NSLog(@"SearchViewController - reloadData");
reloading = YES;
}
- (void)doneLoadingData{
NSLog(@"SearchViewController - doneLoadingData");
reloading = NO;
[refreshView viewDataDidFinishedLoading:self.mainView];
}