当我加载我的主视图时,它会自动加载包含博客文章的JSON供稿。为什么这个UIButton/IBAction不能刷新我的页面?
我在主视图的顶部栏上有一个刷新按钮。我已经成功将它连接到IBAction
,点击后,我可以输出一个字符串进行登录。
我想让我的视图重新加载JSON提要,当我点击刷新按钮,但不起作用。
我在做什么错?
我ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UICollectionViewController {
NSArray *posts;
}
- (void)fetchPosts;
- (IBAction)refresh:(id)sender;
@end
我ViewController.m
...
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchPosts];
}
- (IBAction)refresh:(id)sender {
[self fetchPosts];
}
- (void)fetchPosts
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://website.com/app/"]];
NSError* error;
posts = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
});
}
...
是否成功下载数据? – DrummerB
你想更新UILabel吗?你的UILabel在哪里?它在另一个视图控制器中吗?你想刷新整个视图控制器吗?视图控制器在切换到另一个时进行“刷新”。 – stackOverFlew
您正在尝试重新加载视图而不实际重新加载它? collectionView是否已成功连接到接口构建器引用插座的UITableView?请提供更多信息和更多的代码。 – stackOverFlew