如何在 applicationDidBecomeActive 中的 UIButton 中重新加载图像
我有一个 MenuViewController 在应用程序加载时加载;它是 UINavigationController 的根视图。
在视图中,我有一个 UIButton,其中包含从 URL 加载的图像以及标签(它是一张指示天气和当前温度的图片)。
一切正常,但如果我在 iPhone 4 上使用该应用程序,并进行多任务处理,当按下主页按钮然后重新打开该应用程序时,我需要 UIButton 图像和温度。重新加载标签。
我尝试在我的 AppDelegate 中的 applicationDidBecomeActive: 下调用它,
[self parseWeatherXML]; //re-parse weather data
[menuViewController reloadWeatherData]; //loads the image/label from the XML
但没有成功。
我还尝试在 applicationWillResign 中释放 menuViewController,然后在 applicationDidBecomeActive 中重新分配 menuViewController,以便再次调用 viewDidLoad 方法,这两种方法最终都会导致我的应用程序崩溃。
任何帮助表示赞赏!
编辑 这是我通过通知调用的方法:
- (void)reloadWeather
{
[self parseWeatherXML];
UIImage *img = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:iconURL]]];
if (img != nil)
{
NSLog(@"setting image");
[weatherButton setImage:img forState:normal];
img = nil;
}
currentTemp = [currentTemp stringByAppendingString:@"°F"];
[tempLabel setText:currentTemp];
tempLabel.adjustsFontSizeToFitWidth = YES;
if([self isIPad])
{
tempLabel.textAlignment = UITextAlignmentRight;
[tempLabel setFont: [UIFont systemFontOfSize: 45.0]];
}
currentTemp = nil;
}
I have a MenuViewController that loads when the app loads up; it is the root view of a UINavigationController.
Within the view I have a UIButton with an image that loads from a URL, as well as a label (its a picture indicating the weather and the current temp.).
Everything works fine, but if I am using the app on an iPhone 4, with multi-tasking, when the home button is pressed and then the app is reopened, I need the UIButton image and temp. label to reload.
I have tried calling this in my AppDelegate under applicationDidBecomeActive:
[self parseWeatherXML]; //re-parse weather data
[menuViewController reloadWeatherData]; //loads the image/label from the XML
with no luck.
I have also tried releasing menuViewController in applicationWillResign, then reallocating menuViewController in applicationDidBecomeActive so the viewDidLoad method gets called again, both ways just end up crashing my app.
Any help appreciated!
EDIT
Heres my method that gets called with the notification:
- (void)reloadWeather
{
[self parseWeatherXML];
UIImage *img = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:iconURL]]];
if (img != nil)
{
NSLog(@"setting image");
[weatherButton setImage:img forState:normal];
img = nil;
}
currentTemp = [currentTemp stringByAppendingString:@"°F"];
[tempLabel setText:currentTemp];
tempLabel.adjustsFontSizeToFitWidth = YES;
if([self isIPad])
{
tempLabel.textAlignment = UITextAlignmentRight;
[tempLabel setFont: [UIFont systemFontOfSize: 45.0]];
}
currentTemp = nil;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会让您的 MenuViewController 成为 UIApplicationDidBecomeActiveNotification 的观察者并执行 parseWeatherXML 数据。我认为您会刷新 URL 中的数据,因此您需要等待该数据再次返回。因此,我会遵循您收到该通知时所做的相同逻辑。
I would have your MenuViewController become an observer to the UIApplicationDidBecomeActiveNotification and perform the parseWeatherXML data. I would think you would be refreshing the data from the URL, so you would need to wait for that data to come back again. So, I would follow the same logic that you are doing when you receive that notification.
您是否尝试过在 applicationWillResign 期间使用 plist 或其他内容保存图像和临时文件,然后在 applicationDidBecomeActive 期间,您可以从保存的文件/标签重新加载图像和临时文件。只是探索的另一种选择。
Have you tried saving the image and temp using a plist or something, during the applicationWillResign, and then during applicationDidBecomeActive, you can reload the image and temp from the saved file/label. Just another option to explore.