更改后退按钮的标题

发布于 2024-12-09 06:30:29 字数 965 浏览 0 评论 0原文

我需要将导航控制器中后退按钮的默认标题值更改为其他值。

我正在尝试使用以下代码:

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];

该解决方案的问题是我需要创建一个单独的方法来弹出视图控制器。另一个问题是按钮的形状,我需要一个箭头形状的按钮。

你有什么想法吗?

I need to change the default title value of the back button in a navigationcontroller to something else.

I am trying this with the following code:

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Terug" style:    UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = newBackButton;
[newBackButton release];

i also tried:

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Terug" style:    UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = newBackButton;
[newBackButton release];

I am using this code at the end of the viewDidLoad.

I am able to create a normal button (square) with the following code:

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Terug" style:    UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.leftBarButtonItem = newBackButton;
[newBackButton release];

The problem with this solution is that I need to create a seperate method that pop's the view controller. Another issue would be the shape of the button, I need an arrow shaped button.

do you have any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单身情人 2024-12-16 06:30:29

如果您想要一个自定义后退按钮,您需要创建一个新按钮并为其分配一个新选择器。
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];
}

If you want to have a custom back button you need to create new one and assign a new selector to it.
The backBarButtonItem is not editable, so you need to set the leftBarButtonItem.

This is how you can create a custom back button

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];

And this is the selector to simulate the back button

- (IBAction)backAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
紫瑟鸿黎 2024-12-16 06:30:29

有一个简单的解决方案,

假设您要从 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];
}

There is a simple solution

lets say you are moving from viewA to viewB
just before moving from viewA to viewB change the title of viewA

for example:

-(void)moveToViewB
{

    // Add this line
    self.title = @"Terug"; 
    // and push the new view as normal 
    [self.navigationController pushViewController:viewB animated:NO];
}
眼眸里的那抹悲凉 2024-12-16 06:30:29

从“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;
}

[或者,使其成为“实用程序”类的类方法,并在需要的每个 viewContorller 中将其作为类方法调用。]

另外,不要忘记实现“backAction”,如下所示:

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

快乐编码!

Call this button from your 'viewDidLoad' method.

- (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;
}

[Or, make it a class method of a 'Utitlity' class and call it as a class method in each viewContorller it is required.]

Also, don't forget to implement the 'backAction', something like this:

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

Happy coding!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文