【问题标题】:How to reload image in UIButton in applicationDidBecomeActive如何在 applicationDidBecomeActive 的 UIButton 中重新加载图像
【发布时间】:2011-07-06 06:08:13
【问题描述】:

我有一个在应用程序加载时加载的 MenuViewController;它是 UINavigationController 的根视图。

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

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

我尝试在我的 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;
}

【问题讨论】:

  • 你能把控制台消息粘贴到这里吗?
  • @robin 我得到了 EXE_BAD_ACCESS。加载时我不正确,但是在我按下主页并重新启动应用程序几次之后。出于某种原因,标签在应该显示温度时显示 MenuViewController。
  • 在此处粘贴控制台输出。

标签: iphone ios uibutton reload multitasking


【解决方案1】:

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

UIApplicationDidBecomeActiveNotification

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

可用性

适用于 iOS 2.0 及更高版本。

声明于

UIApplication.h

【讨论】:

  • @bstahlhood 我已经实现了你的建议如下:'[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWeather) name:@"UIApplicationDidBecomeActiveNotification" object:nil ];'我把它放在 MenuViewController 的 viewDidLoad 方法中。我已经 reloadWeather 打印了一个日志,它工作了 1-2 次(按 home 然后重新打开),但 1-2 次后它得到 EXE_BAD_ACCESS。我的猜测是 reloadWeather 在 parseWeatherXML 之后被调用,它在 applicationDidBecomeActive 中调用。
  • 我尝试将代码放在 's 中,但它不起作用,抱歉,看起来一团糟。
  • @bstahlhood 通知方法完美;我使图像和标签不加载启动,仅在调用 viewDidLoad 时才加载,仅在应用程序关闭一次后重新打开时才调用。所以我知道通知正在工作,但是就像我说的,一旦我用加载的图像重新打开应用程序,如果我再次尝试关闭它,我会得到 EXE_BAD_ACCESS。 appDelegate 中有什么我没有处理的吗?此错误仅发生在其中的通知代码中。
  • @Ryan Garchinsky 很高兴我能帮上忙 :)
  • @bstahlhood 我以为我已经解决了问题,但我没有。我使用 UIApplicationDidBecomeActiveNotification 通知调用的方法被调用,但它不会刷新 UIButton 图像或 UILabel。在该方法中,我首先解析新的天气数据,我在控制台中打印出来并对其进行解析;我还将 UIButton 图像设置为新解析的图像并将标签设置为新解析的温度,但就像我说的旧图像/温度仍然存在。
【解决方案2】:

您是否尝试过使用 plist 或其他方式保存图像和临时文件,在 applicationWillResign 期间,然后在 applicationDidBecomeActive 期间,您可以从保存的文件/标签中重新加载图像和临时文件。只是另一种探索方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2014-01-08
    • 2011-01-07
    • 1970-01-01
    • 2020-11-27
    相关资源
    最近更新 更多