我有一个与我的 View Controller 通信的 EventsManager 类。我希望能够更新 UIView
通过从 EventManager 类调用我的 View 中的方法(例如 updateProgressBar)来获取元素(图像、进度条等)。
但是,每当我尝试更新 UIView
我认为除了 viewDidLoad
之外的任何方法中的元素,它完全被忽略了。
有什么我想念的吗?
super 简单的例子:
这有效
- (void)viewDidLoad
{
progressBar.progress = 0.5;
}
这没有(这个方法在我的 View Controller 中)
- (void)updateProgressBar:(float)myProgress
{
NSLog(@"updateProgressBar called.");
progressBar.progress = myProgress;
}
所以,如果我打电话:
float currentProgress = 1.0;
ViewController *viewController = [[ViewController alloc] init];
[viewController updateProgressBar:currentProgress]
来自我的 EventsManager 类(class),
updateProgressBar
被调用(用断点证明),但进度条更新被忽略。没有错误或异常抛出。和 updateProgressBar called.
显示在控制台中。
最佳答案
您可以做的是为进度条更新添加一个 NSNotification 并从您想要的任何地方调用它。
在您的 ViewController 的 viewDidLoad 添加这个观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(progressBarUpdater:) name:@"progressBarUpdater" object:nil];
然后添加以下方法
-(void)progressBarUpdater:(float)currentProgress
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"progressBarUpdater" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:currentProgress,@"progress", nil]];
}
并更新您的方法
- (void)updateProgressBar:(NSNotification *)notificaiton
{
NSLog(@"updateProgressBar called.");
NSDictionary *dict = [notificaiton userInfo];
progressBar.progress = [dict valueForKey:@"progress"];
// progressBar.progress = myProgress;
}
关于ios - 改变 viewDidLoad 之外的 UIView 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19532909/