gpt4 book ai didi

iphone - 使用 UIRefreshControl

转载 作者:行者123 更新时间:2023-11-28 18:36:29 28 4
gpt4 key购买 nike

目前,我有一个正在使用 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'

我很困惑:(

最佳答案

问题:

出现这个问题是因为:

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

您已添加 endRefresh: 作为计时器的选择器。所以 endRefresh: 方法的参数将是 NSTimer 而不是 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];
}

解决方案一:

UIRefreshControl 声明为属性并使用它。

方案二:

改变你的方法,比如:

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

关于iphone - 使用 UIRefreshControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17938225/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com