我需要将导航控制器中后退按钮的默认标题值更改为其他内容。
我正在尝试使用以下代码:
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Terug" style: UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = newBackButton;
[newBackButton release];
我也试过了:
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Terug" style: UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = newBackButton;
[newBackButton release];
我在viewDidLoad的末尾使用此代码。
我可以使用以下代码创建一个普通按钮(方形):
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Terug" style: UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.leftBarButtonItem = newBackButton;
[newBackButton release];
这个解决方案的问题是我需要创建一个弹出视图控制器的单独方法。另一个问题是按钮的形状,我需要一个箭头形按钮。
你知道吗?答案 0 :(得分:1)
如果您想要一个自定义后退按钮,您需要创建一个新按钮并为其指定一个新的选择器。 backBarButtonItem不可编辑,因此您需要设置leftBarButtonItem。
这是您创建自定义后退按钮的方法
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:NSLocalizedString(@"Back", nil) forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[button addTarget:navigationController action:@selector(backAction:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
navigationController.navigationItem.leftBarButtonItem = backButton;
[backButton release];
这是模拟后退按钮的选择器
- (IBAction)backAction:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
答案 1 :(得分:0)
有一个简单的解决方案
假设您正在从viewA移动到viewB 在从viewA移动到viewB之前,更改viewA的标题
例如:
-(void)moveToViewB
{
// Add this line
self.title = @"Terug";
// and push the new view as normal
[self.navigationController pushViewController:viewB animated:NO];
}
答案 2 :(得分:0)
从您的' viewDidLoad'中调用此按钮方法
- (void)addCustomBackButton {
/* enter your image name and size here */
UIImage *backImage = [UIImage imageNamed:@"back_btn.png"];
CGRect buttonRect = CGRectMake(5.0f, 0.0f, 12.0f, 20.0f);
/* enter your image name and size here */
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; // implemeent 'backAction' method in each VC
[backButton setFrame:buttonRect];
UIImageView *imageView = [[UIImageView alloc] initWithImage:backImage];
[imageView setFrame:buttonRect];
[backButton addSubview:imageView];
UIBarButtonItem *homeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = homeButtonItem;
}
[或者,让它成为一个' Utitlity' class并在每个viewContorller中将其称为类方法。它是必需的。]
另外,不要忘记实施' backAction',如下所示:
- (void)backAction
{
[self.navigationController popViewControllerAnimated:YES];
}
快乐的编码!