2014-12-03 24 views
1

我正在嘗試更改導航欄後退按鈕文本的文本。我已經嘗試過這裏提供的解決方案,但我不能在後退按鈕上獲得我想要的文本。在我的appdelegate設置這些功能:導航欄後退按鈕文本不變

[[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]]; 
[[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]]; 

[[UINavigationBar appearance] setTitleTextAttributes: 
[NSDictionary dictionaryWithObjectsAndKeys: 
    [UIColor whiteColor], UITextAttributeTextColor, 
    [UIFont fontWithName:@"Futura-CondensedMedium" size:19.0], UITextAttributeFont,nil]]; 

NSShadow *shadow = [[NSShadow alloc] init]; 
shadow.shadowOffset = CGSizeMake(0.0, 1.0); 
shadow.shadowColor = [UIColor whiteColor]; 

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] 
setTitleTextAttributes: 
@{NSForegroundColorAttributeName:[UIColor whiteColor], 
    NSShadowAttributeName:shadow, 
    NSFontAttributeName:[UIFont fontWithName:@"Futura-CondensedMedium" size:19.0] 
    } 
forState:UIControlStateNormal]; 

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; 

,並在每個視圖 - 控制的viewdidload我設置:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"SomeText" style:UIBarButtonItemStylePlain target:nil action:nil]; 

UIBarButtonItem *newBackButton = 
     [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
             style:UIBarButtonItemStyleBordered 
             target:nil 
             action:nil]; 
[[self navigationItem] setBackBarButtonItem:newBackButton]; 

有什麼建議?

回答

4

如果我沒有誤會你,你能做到這一點,設置後退按鈕的標題。假設有三個視圖控制器:ABC。現在,當您從A導航到B時,您希望後退按鈕的標題爲「MyViewA」,那麼您需要將此代碼放入您的ViewController A的「viewWillAppear:」中(這會將後退按鈕的標題顯示爲「MyViewA 「當你在查看控制器B)。

//viewWillAppear of ViewController A 
-(void)viewWillAppear:(BOOL)animated{ 

     //now set title of back button 
     self.navigationItem.backBarButtonItem = 
     [[UIBarButtonItem alloc] initWithTitle:@"YourTitle" 
            style:UIBarButtonItemStyleBordered 
           target:nil 
           action:nil]; 
} 

同樣,如果你從B導航到C並要設置後退按鈕標題視圖控制器C,你需要把上述代碼「viewWillAppear中:」視圖控制器B的代表。總之,您需要在當前視圖控制器上設置下一個視圖控制器的標題。

+0

謝謝你。它的工作現在! – birdcage 2014-12-04 07:09:57

1

您不能更改後退按鈕中文本的值。你必須設置按鈕的左邊或右邊barButtonItem(如果你想把它當作後退按鈕可能離開)是這樣的:

[self.navigationItem setLeftBarButtonItem:newBackButton]; 

你可以通過設置它,當你初始化你的按鈕,然後附加一個IBAction爲到按鈕:

UIBarButtonItem *newBackButton = 
    [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
            style:UIBarButtonItemStyleBordered 
            target:nil 
            action:yourBackFunction]; 

至少這將是一個解決辦法,我也使用過它...

1

後退按鈕屬於上一個視圖控制器,而不是當前在屏幕上顯示的按鈕。 要修改後退按鈕,你應該在推動之前更新它,在啓動賽格的視圖控制器上:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Delete back item title in next controller 
    UIBarButtonItem *newBackButton = 
    [[UIBarButtonItem alloc] initWithTitle:@"New Title" 
            style:UIBarButtonItemStylePlain 
            target:nil 
            action:nil]; 
    [[self navigationItem] setBackBarButtonItem:newBackButton]; 
}