2 vikeytoy vikeyToy 于 2013.07.30 15:26 提问

使用UIRefreshControl

应用中用到UIRefreshControl

遇到一些问题,请大神解答。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.searchDisplayController.delegate = self;
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    [self.rearTableView addSubview:refreshControl];
}
- (void)refresh:(UIRefreshControl *)refreshControl {
    [refreshControl beginRefreshing];
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(endRefresh:) userInfo:nil repeats:NO];
}
- (void)endRefresh:(UIRefreshControl *)refresh
{
    [refresh endRefreshing];
}

拖拽tableView会初始化计时器,但是提前2秒。然后应用就崩溃了:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer endRefreshing]: unrecognized selector sent to instance 0x9c093c0'

2个回答

zhanglei5415
zhanglei5415   Rxr 2013.07.30 16:38

NSTimer的处理selector中的方法命名不妥。改成

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(endRefresh) userInfo:nil repeats:NO];

- (void)endRefresh
{
    [refresh endRefreshing];
}

将refreshControl在头文件中定义成成员变量。

Chengzi_963
Chengzi_963   2013.07.30 16:41

问题出在这里:

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(endRefresh:) userInfo:nil repeats:NO];

你添加了endRefresh作为计数器的selector。因此endRefresh的参数应该是一个NSTimer ,而不是UIRefreshControl

解决方法1

将UIRefreshControl 作为property声明。

解决方法2

修改方法如下:

- (void)refresh:(UIRefreshControl *)refreshControl
{
    [refreshControl beginRefreshing];
    [self performSelector:@selector(endRefresh:) withObject:refreshControl afterDelay:2.0f];
}

- (void)endRefresh:(UIRefreshControl *)refresh
{
    [refresh performSelectorOnMainThread:@selector(endRefreshing) withObject:nil waitUntilDone:NO];
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!