cocoapods와 함께 SVProgressHUD를 사용하려고합니다. 온라인으로 수많은 자습서를 따라 xcode 작업 공간에 설치했습니다. 사용하기에 충분히 단순 해 보이지만 작동시키지는 못합니다. 여기 내 코드는 rottentomatoes 응용 프로그램에서 데이터를로드합니다. 네트워크 요청이 그 일을하는 동안 "로딩"을 보여주고 싶습니다. 이것이 ui 스레드 문제일까요? 결과가 너무 빨리 다시 나오기 때문에 네트워크 요청 내부에 잠을 더했습니다! 코멘트svprogresshud가 xcode5에 표시되지 않습니다. ios7
- (void) reload{
[SVProgressHUD showWithStatus:@"Updating" maskType:SVProgressHUDMaskTypeBlack];
NSString *url = @"http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/top_rentals.json?";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
[NSThread sleepForTimeInterval:3];
NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[object objectForKey:@"movies"]];
self.movies = [[NSMutableArray alloc]init];
for(NSDictionary *item in array){
SFMovie *movie = [[SFMovie alloc]initWithDictionary:item];
[self.movies addObject:movie];
}
[SVProgressHUD dismiss];
[self.tableView reloadData];
}];
}
편집 :는
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self reload];
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
[self reload];
}
return self;
}
두 번째 편집 (나는이 프로젝트에 스토리 보드를 사용하고 있기 때문에) 다시로드가 모두 초기화 방법에라고 : 나는 아래로 끌어 오기 추가 내가 테이블 뷰를 끌어 올 때 새로 고치고 "업데이트 중 ..."이 표시됩니다. 하지만 init에는 나타나지 않습니다.
- (void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refreshControl = [UIRefreshControl new];
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh..."];
self.refreshControl = refreshControl;
}
- (void)refresh:(UIRefreshControl *)sender{
[self reload];
[sender endRefreshing];
}
그래서 저는 뷰가 준비되지 않았기 때문에 init 메소드에서 UI를 실행할 수 없다고 생각합니다. viewdidload에 reload 메소드를 호출하고 있습니다. 존 감사합니다!
메인 스레드에서'reload'가 호출 되었습니까? – John
@ John reload가 init 메소드에서 호출되므로 UI 스레드라고 생각합니다. 고맙습니다. –
이것은 답변과는 완전히 관련이 없지만이 로직을'viewDidLoad'로 옮길 것을 제안합니다 -이 시점에서'tableView'가 정확히 준비되지 않았기 때문에'[self.tableView reloadData]'로 요청을 콜백은 경쟁 조건을 발생시킵니다. – John