iphone - 以编程方式将导航栏按钮更改为活动指示器 [英] iphone - programatically change navigation bar button to activity indicator

查看:100
本文介绍了iphone - 以编程方式将导航栏按钮更改为活动指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的iPhone应用程序的导航栏上添加了刷新UIBarButtonItem。当用户点击按钮时,我希望刷新按钮更改为动画活动指示器,一旦操作(在这种情况下是下载)完成,将活动指示器切换回刷新按钮。

I have added a refresh UIBarButtonItem to my navigation bar on my iPhone app. When the user taps the button I'd like the refresh button to change to the animated activity indicator and once the operation (in this case a download) is complete switch the activity indicator back to the refresh button.

我已经使用IB添加了刷新按钮。然后在按钮上点击我创建一个新的活动指示器并保持指向原始刷新按钮的指针。像这样:

I have added the refresh button using IB. Then on the button tap I create a new activity indicator and keep an pointer to the original refresh button. Like so:

refreshButtonItem = self.navigationItem.leftBarButtonItem;
if (activityButtonItem == nil)
{
    activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20,20)];
    activityButtonItem = [[UIBarButtonItem alloc]initWithCustomView:activityIndicator];

}
self.navigationItem.leftBarButtonItem = activityButtonItem;
[activityIndicator startAnimating];

到目前为止,非常好。问题是,当我的下载完成并尝试重新添加刷新按钮时(使用以下内容):

So far, so good. Problem is that when my download finishes and I try to re-add the refresh button (using the following):

[activityIndicator stopAnimating];
self.navigationItem.leftBarButtonItem = refreshButtonItem;

我收到以下错误:

[UIBarButtonItem retain]:发送给的消息解除分配的实例

I get the following error:
[UIBarButtonItem retain]: message sent to deallocated instance

我没有明确地调用release。

I'm not explicitly calling release.

A)何时/何地被解除分配

A) When/where is this being deallocated

B)是否有更好的方法来实现我的目标正在寻找?

B) Is there a better way to achieve what I'm looking for?

推荐答案

当您将activityButtonItem分配给leftBarButtonItem时,将释放leftBarButtonItem用于指向的项目。 leftBarButtonItem(以及带有retain选项的所有属性)的实现与此类似:

When you assign the activityButtonItem to leftBarButtonItem, the item that leftBarButtonItem used to point to is released. The leftBarButtonItem (and all properties with the retain option) are implemented similarly to this:

- (void)leftBarButtonItem:(UIBarButtonItem *)newItem {
  if (newItem != self.leftBarButtonItem) {
    [self.leftBarButtonItem release];
    leftBarButtonItem = [newItem retain];
  }
}

如果要在重新分配leftBarButtonItem后使用refreshButtonItem ,将您的第一行更改为:

If you want to use the refreshButtonItem after reassigning the leftBarButtonItem, change your first line to:

refreshButtonItem = [self.navigationItem.leftBarButtonItem retain];

这篇关于iphone - 以编程方式将导航栏按钮更改为活动指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆