我在我的GUI上使用setNeedsDisplay,但有时没有更新.我正在使用UIPageControllView,每个页面都有UIScrollView和UIView里面.
我有以下管道:
1)应用程序来自后台 – 名为applicationWillEnterForeground
2)从服务器开始下载数据
2.1)数据下载完成后,触发选择器
3)使用dispatch_async和dispatch_get_main_queue()来填充新数据的标签,图像等
3.1)在视图上调用setNeedsDisplay(也尝试滚动视图和页面控制器)
问题是,调用了步骤3.1,但只是不时地改变apper.如果我交换页面,刷新完成,我可以看到新数据(所以下载工作正常).但是没有手动翻页,就没有更新.
有帮助吗?
编辑:步骤3和3.1中的代码(删除了注释中指向的_needRefresh变量)
-(void)FillData { dispatch_async(dispatch_get_main_queue(),^{ NSString *stateID = [DataManager ConvertStateToStringFromID:_activeCity.actual_weather.state]; if ([_activeCity.actual_weather.is_night boolValue] == YES) { self.contentBgImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"bg_%@_noc",[_bgs objectForKey:stateID]]]; if (_isNight == NO) { _bgTransparencyInited = NO; } _isNight = YES; } else { self.contentBgImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"bg_%@",[_bgs objectForKey:stateID]]]; if (_isNight == YES) { _bgTransparencyInited = NO; } _isNight = NO; } [self.contentBgImage setNeedsDisplay]; //refresh background image [self CreateBackgroundTransparency]; //create transparent background if colors changed - only from time to time self.contentView.parentController = self; [self.contentView FillData]; //Fill UIView with data - set labels texts to new ones //_needRefresh is set to YES after application comes from background [self.contentView setNeedsDisplay]; //This do nothing ? [_grad display]; //refresh gradient }); }
这里是数据下载后调用的选择器(在MainViewController中)
-(void)FinishDownload:(NSNotification *)notification { dispatch_async(dispatch_get_main_queue(),^{ [_activeViewController FillData]; //call method shown before //try call some more refresh - also useless [self.pageControl setNeedsDisplay]; //[self reloadInputViews]; [self.view setNeedsDisplay]; }); }
在AppDelegate中我有这个应用程序来自后台:
-(void)applicationWillEnterForeground:(UIApplication *)application { MainViewController *main = (MainViewController *)[(SWRevealViewController *)self.window.rootViewController frontViewController]; [main UpdateData]; }
在MainViewController中
-(void)UpdateData { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FinishForecastDownload:) name:@"FinishDownload" object:nil]; //create selector [[DataManager SharedManager] DownloadForecastDataWithAfterSelector:@"FinishDownload"]; //trigger download }
解决方法
试试这个:
[self.view performSelectorOnMainThread:@selector(setNeedsLayout) withObject:nil waitUntilDone:NO];
或检查此链接:
http://blackpixel.com/blog/2013/11/performselectoronmainthread-vs-dispatch-async.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。