开发者

Change title of backbutton

开发者 https://www.devze.com 2023-04-12 19:08 出处:网络
I need to change the default title value of the back button in a navigationcontroller to something else.

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 = newBack开发者_运维百科Button;
[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?


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


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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号