向导航栏添加刷新按钮

时间:2013-12-06 01:43:49

标签: ios iphone objective-c uitableview

大家好我很痛苦的时候试图让这个导航栏包含一个刷新按钮,如果你可以查看下面的代码,然后提供一些帮助这是我的原始代码以及我发现的UIBarbutton片段来自谷歌上其他用户项目的刷新按钮。我遇到了以下错误:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView setBackgroundView:nil];
    self.tableView.backgroundColor = [ColorHelper charcoalBackgroundImage];
    UIBarButtonItem *refresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
    self.navigationItem.leftBarButtonItem = refresh;
    // [button release]; remove this line if you're using ARC
}
- (void) refresh
{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

5 个答案:

答案 0 :(得分:6)

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView setBackgroundView:nil];
    self.tableView.backgroundColor = [ColorHelper charcoalBackgroundImage];

    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
    self.navigationItem.rightBarButtonItem = button;
}

将此方法编写为此方法,因为您正在调用UIBarButton对象。

- (void) refresh:(id)sender
{

}

UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];

答案 1 :(得分:1)

可能这段代码会帮助你...

UIImage *reloadImage = [UIImage imageNamed:@"reload.png"];
UIButton *btnReload = [UIButton buttonWithType:UIButtonTypeCustom];
btnReload.bounds = CGRectMake( 10, 0, reloadImage.size.width, reloadImage.size.height );
[btnReload addTarget:self action:@selector(reloadData:) forControlEvents:UIControlEventTouchUpInside];
[btnReload setImage:reloadImage forState:UIControlStateNormal];
UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithCustomView:btnReload];
self.navigationItem.leftBarButtonItem = reloadButton;

答案 2 :(得分:0)

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView setBackgroundView:nil];
    self.tableView.backgroundColor = [ColorHelper charcoalBackgroundImage];

    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
    self.navigationItem.rightBarButtonItem = button;
    // [button release]; remove this line if you're using ARC
}

您似乎已经在使用ARC了。因此,您应该删除[button release]。你的书正在走向历史。

答案 3 :(得分:0)

您的动作选择器和实际方法不一样。一个是refresh:,另一个是refresh

更改选择器以匹配实际方法。

@selector(refresh)

答案 4 :(得分:0)

快速4

let refreshButton = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(refresh))
self.navigationItem.rightBarButtonItem = refreshButton

@objc func refresh() {

}