0

目前,我有一个应用程序正在使用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 确实会初始化,timer2秒到了......我的应用程序crashes并发送此消息:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer endRefreshing]: unrecognized selector sent to instance 0x9c093c0'

我很困惑 :(

4

1 回答 1

5

问题:

出现此问题的原因是:

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

您被添加endRefresh:为计时器的选择器。所以该endRefresh:方法的参数将是NSTimernot UIRefreshControl

实际的方法签名将如下所示:

- (void)endRefresh:(NSTimer *)refresh
{
   //your code
}

您正在调用该endRefreshing NSTimer对象,这就是发生崩溃的原因。

声明如下:

- (void)endRefresh:(UIRefreshControl *)refresh

只需将NSTimer对象类型转换为仅此UIRefreshControl而已。

它相当于:

- (void)endRefresh:(NSTimer *)timer
{
   UIRefreshControl *refresh = (UIRefreshControl *)timer;
   [refresh endRefreshing];
}

解决方案1:

声明UIRefreshControl为属性并使用它。

解决方案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];
}
于 2013-07-30T04:24:06.537 回答