iphone - 如何在 applicationDidBecomeActive 的 UIButton 中重新加载图像

标签 iphone ios uibutton reload multitasking

我有一个 MenuViewController,它会在应用程序加载时加载;它是 UINavigationController 的 Root View 。

在 View 中,我有一个 UIButton,它带有一个从 URL 加载的图像,以及一个标签(它是一张指示天气和当前温度的图片。)。

一切正常,但如果我在 iPhone 4 上使用应用程序,多任务处理,当按下主页按钮然后应用程序重新打开时,我需要 UIButton 图像和温度。要重新加载的标签。

我已经尝试在 applicationDidBecomeActive 下的 AppDelegate 中调用它:

[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;
}

最佳答案

我会让您的 MenuViewController 成为 UIApplicationDidBecomeActiveNotification 的观察者并执行 parseWeatherXML 数据。我认为您会刷新 URL 中的数据,因此您需要等待该数据再次返回。因此,当您收到该通知时,我会遵循您正在执行的相同逻辑。

UIApplicationDidBecomeActiveNotification

Posted when the application becomes active. An application is active when it is receiving events. An active application can be said to have focus. It gains focus after being launched, loses focus when an overlay window pops up or when the device is locked, and gains focus when the device is unlocked.

Availability

Available in iOS 2.0 and later.

Declared In

UIApplication.h

关于iphone - 如何在 applicationDidBecomeActive 的 UIButton 中重新加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5150605/

相关文章:

ios - 使用联系人框架创建新组,CNErrorDomain Code = 2

iphone - 无法更改选定状态的自定义 uiButton 的背景图像

iphone - UIWebView shouldStartLoadWithRequest 仅在从内部调用模态视图时触发一次

ios - 尝试拖放对象时旋转 UIView iOS 错误

iphone - 在 iOS5 中使用 Storyboard 时,如何将数据从一个文本字段传递到另一个文本字段?

ios - 检索 UITableViewCell 框架

ios - UITableview 中的按钮修改当前单元格自定义

iphone - UIBUTTON 和选择器 View

iphone - 在 UITableViewCell 图像上设置 UIImage 尺寸

iphone - 外观 API - UIBarButtonItem - MPMoviePlayerViewController 和 Youtube Webview