2011-10-11 94 views
2

我需要將導航控制器中後退按鈕的默認標題值更改爲其他值。更改後臺按鈕的標題

我用下面的代碼嘗試這樣的:

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

這種解決方案的問題是,我需要創建一個彈出的視圖控制器一個單獨的方法。另一個問題將是按鈕的形狀,我需要一個箭頭形按鈕。

你有什麼想法嗎?

回答

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

[OR,使之成爲「Utitlity」類中的一個方法,並把它作爲每個viewContorller它需要一個類的方法。]

而且,不要忘了貫徹落實「 backAction',像這樣:

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

快樂編碼!