代表和方法没有按预期工作

时间:2012-09-06 08:19:38

标签: iphone objective-c ios xcode uiscrollviewdelegate

我一直在尝试修复我正在处理的应用程序的问题。在这里搜索和浏览问题后,我认为代表和一些方法存在问题。

该代码用于实现“下拉​​刷新”,UIVc嵌入在UIScrollView中。

我正在实施的课程:

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];

}

1 个答案:

答案 0 :(得分:1)

试试这个。你的RefreshView.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

typedef enum{

PullRefreshPulling = 0,
PullRefreshNormal,
PullRefreshLoading,

} PullRefreshState;

@class RefreshView
@protocol RefreshViewDelegate <NSObject>
@required
- (void)didTriggerRefresh:(RefreshView*)view;
- (BOOL)dataIsLoading:(RefreshView*)view;
@end

@interface RefreshView : UIScrollView <UIScrollViewDelegate> {

id <RefreshViewDelegate> refreshDelegate;
PullRefreshState state;

UILabel *lastUpdatedLabel;
UILabel *statusLabel;
CALayer *arrowImage;
UIActivityIndicatorView *activityView;

}

@property(nonatomic,strong) id <RefreshViewDelegate> refreshDelegate;

- (void)viewDidScroll:(UIScrollView *)scrollView;
- (void)viewDidEndDragging:(UIScrollView *)scrollView;
- (void)viewDataDidFinishedLoading:(UIScrollView *)scrollView;

@end

在您的RefreshView.m

@systhesis refreshDelegate;