xcode 重新加载或刷新 UIView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9306010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reloading or refreshing a UIView?
提问by user339946
My app allows a user to unlock a bunch of levels with an in app purchase. To indicate that a level is locked, I change the icon alpha to 0.7, just to differentiate it from levels they own/can access.
我的应用程序允许用户通过应用程序内购买来解锁一堆关卡。为了指示某个级别已锁定,我将图标 alpha 更改为 0.7,只是为了将其与他们拥有/可以访问的级别区分开来。
My code works by checking the NSUserDefaults for a BOOL key on viewDidLoad:
我的代码通过在 viewDidLoad 上检查 NSUserDefaults 的 BOOL 键来工作:
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"com.productbundle.unlock"]){
//set these levels to alpha = 0.7
}
So after they successfully purchase, it would be really easy if I could just refresh/reload the UIView. Is there a way to do this? Or is there alternate way I can achieve this affect? Thanks
所以在他们成功购买后,如果我可以刷新/重新加载 UIView 就真的很容易了。有没有办法做到这一点?或者有没有其他方法可以实现这种影响?谢谢
回答by AlexTheMighty
I don't think you should be calling [self viewDidLoad]. As I understand it those are more methods you implement in your subclass to respond to various events and in general you shouldn't call them yourself. Looking more at the documentation for views, if you want to refresh stuff when a view becomes visible again look into implementing
我认为您不应该调用 [self viewDidLoad]。据我了解,这些是您在子类中实现的更多方法来响应各种事件,通常您不应该自己调用它们。查看更多视图文档,如果您想在视图再次可见时刷新内容,请查看实现
-(void)viewDidAppear:(BOOL)animated
回答by jhilgert00
I would put it in -(void)viewWillAppear:(BOOL)animated
我会把它放进去 -(void)viewWillAppear:(BOOL)animated
viewWillAppear = "Notifies the view controller that its view is about to be become visible."
" viewWillAppear = "通知视图控制器它的视图即将变为可见。
I'd check for your user defaults, and stick an if statement in there:
我会检查您的用户默认值,并在其中粘贴一个 if 语句:
- (void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:@"com.productbundle.unlock"] == YES) {
//set alpha to 1
};
[super viewWillAppear:animated];
}
回答by Bob de Graaf
use the setNeedsDisplay to refresh a UIView.
使用 setNeedsDisplay 刷新 UIView。
[yourUIView setNeedsDisplay];