7

我需要为自定义 UIBarButtonItem 设置标题。

我有以下代码:

UIButton* backToRecent = [UIButton buttonWithType:UIButtonTypeCustom];
[backToRecent setImage:[UIImage imageNamed:@"back.png"]forState:UIControlStateNormal];

[backToRecent addTarget:self action:@selector(backToRecent) forControlEvents:UIControlEventTouchUpInside];
[backToRecent setFrame:CGRectMake(0, 0, 60, 30)];

UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backToRecent];
[backButtonItem setTitle:@"Recent"];
partViewController.navigationItem.leftBarButtonItem = backButtonItem;
[backButtonItem release];

但是该按钮出现时没有“最近”文本。

4

2 回答 2

18

穆罕默德,我怀疑你已经解决了这个问题,但这是一个简单的方法。希望这可以帮助遇到同样问题的人:

    UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Recent" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRecent)];
    self.navigationItem.leftBarButtonItem = backButtonItem;
于 2012-12-04T00:38:16.717 回答
7

你应该在 UIButton 上设置标题,而不是 UIBarButtonItem

[backToRecent setTitle:@"Recent" forState:UIControlStateNormal];

而且变量和方法同名也不行

[backToRecent addTarget:self action:@selector(backToRecent) forControlEvents:UIControlEventTouchUpInside];

这是行不通的,因为你使用

[backToRecent setImage:[UIImage imageNamed:@"back.png"]forState:UIControlStateNormal];

尝试使用

[backToRecent setBackgroundImage:[UIImage imageNamed:@"back.png"]forState:UIControlStateNormal];
于 2012-05-24T09:01:13.287 回答