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

查看:50
本文介绍了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保留]:消息发送到已释放实例

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

我没有明确地要求发布.

I'm not explicitly calling release.

A)何时/何处取消

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天全站免登陆