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

发布于 2024-10-19 10:12:34 字数 1246 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烙印 2024-10-26 10:12:34

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

UIApplicationDidBecomeActiveNotification

当申请成为
积极的。应用程序处于活动状态时
它正在接收事件。一个活跃的
应用可以说是有重点的。
上线后备受关注,
覆盖窗口时失去焦点
弹出或当设备锁定时,
并在设备启动时获得焦点
已解锁。

可用性

适用于 iOS 2.0 及更高版本。

声明于

UIApplication.h

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.

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

澜川若宁 2024-10-26 10:12:34

您是否尝试过在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文