[英]Alter UIView elements outside viewDidLoad
我有一個與我的視圖控制器通信的EventsManager類。 我希望能夠通過從EventManager類中調用視圖內部的方法(例如,諸如updateProgressBar)來更新UIView
元素(圖像,進度條等)。
但是,無論何時我嘗試從視圖中除viewDidLoad
之外的任何方法中更新UIView
元素時,都將被完全忽略。
有什么我想念的嗎?
超級簡單的例子:
這有效
- (void)viewDidLoad
{
progressBar.progress = 0.5;
}
這不是(此方法在我的視圖控制器中)
- (void)updateProgressBar:(float)myProgress
{
NSLog(@"updateProgressBar called.");
progressBar.progress = myProgress;
}
所以,如果我打電話:
float currentProgress = 1.0;
ViewController *viewController = [[ViewController alloc] init];
[viewController updateProgressBar:currentProgress]
從我的事件管理類, 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.