从Obj-C中的addTarget动作函数添加回调

时间:2017-09-17 11:15:01

标签: ios objective-c uitableview callback uirefreshcontrol

我为UITableView类编写了类别。在那里,我添加了添加刷新控制器的方法。

我希望我的刷新控制器目标方法给主函数回调。

  

我的tableView类别.h

#import <UIKit/UIKit.h>

typedef void (^UITableViewRefreshControllerCompletion) (UITableView *tableView);

@interface UITableView (UITableView)

-(void)addRefreshController:(UITableViewRefreshControllerCompletion)completionblock;
-(void)removeRefreshController;

@end
  

我的tableView类别.m:

#import "UITableView+UITableView.h"

@implementation UITableView (UITableView)

-(void)addRefreshController:(UITableViewRefreshControllerCompletion)completionblock {

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
    [refreshControl setTintColor:[UIColor appthemeblueColor]];
    [self setRefreshControl:refreshControl];

    [refreshControl addTarget:self action:@selector(refreshTableView:) forControlEvents:UIControlEventValueChanged];

}

-(void) refreshTableView:(UIRefreshControl* )refreshControl {

     completionblock(self); // I want this to call when this method is getting called
}



-(void)removeRefreshController {

    if([self.refreshControl isRefreshing])
        [self.refreshControl endRefreshing];
}
  

我正在调用ViewController中的刷新控制器:

 [self.profileDetailsrTableView addRefreshController:^(UITableView *tableView){

        [self profileDetailsAPICall];
    }];

1 个答案:

答案 0 :(得分:0)

我通过获取UIRefreshControl的子类并存储完成块来解决这个问题;

子类 -

#import <UIKit/UIKit.h>

typedef void (^UITableViewRefreshControllerCompletion) (UITableView *tableView);

@interface UIRefreshControlSubClass : UIRefreshControl

@property(strong, nonatomic) UITableViewRefreshControllerCompletion completionBlock;

@end

所以电话成了:

-(void)addRefreshController:(UITableViewRefreshControllerCompletion)completionblock {

    UIRefreshControlSubClass *refreshControl = [[UIRefreshControlSubClass alloc]init];
    [refreshControl setTintColor:[UIColor appthemeblueColor]];
    [self setRefreshControl:refreshControl];
    refreshControl.completionBlock = completionblock;

    [refreshControl addTarget:self action:@selector(refreshTableView:) forControlEvents:UIControlEventValueChanged];

}

-(void) refreshTableView:(UIRefreshControlSubClass* )refreshControl {

     refreshControl.completionBlock(self);
}