2013-08-21 30 views
6

我想更改UIBarButtonItemtitle爲什麼我不能更改UIBarButtonItem的標題?

但是這段代碼無法正常工作。

- (void)viewDidLoad { 
    ...   
    [self smay]; 
} 

- (void)smay { 
    ... 

    AppDelegate *apd = (AppDelegate*)[[UIApplication sharedApplication]delegate]; 
    NSString *SmayUse = [NSString stringWithFormat:@"%d月%d日", 
          apd.newyear,apd.newmonth]; 
    [_smay setTitle:SmayUse] 
} 

我不知道如何解決這個問題。

請告訴我一種方法來解決這個問題。

+0

您是否檢查SmayUse是否爲零? –

+0

你在哪裏/如何聲明'UIBarButtonItem'我在這個例子中錯過了它。 – rckoenes

回答

-3

item.title = @"new title"應該工作。

+0

OP基本上是在談論不該發生的事情。正如其他人所說,它可能不會被正確初始化,但我不認爲是這樣。我懷疑這個原因甚至是我無法想象的笨重。 –

-1

您必須確保按鈕已初始化。如果它是.xib文件的一部分,請確保已將其與您的@property連接,該文件也有與之關聯的IBOutlet。否則,您需要以編程方式初始化它。另外,您應該使用方法setTitle:forState:

+0

UIBarButtonItem沒有名爲'setTitle:forState:'的方法。 – hgwhittle

5

初始化您的UIBarButtonItem這樣的:

_smay = [[UIBarButtonItem alloc] initWithTitle:@"Your Title" 
      style:UIBarButtonItemStyleBordered 
      target:self action:@selector(yourMethod)]; 

self.navigationItem.rightBarButtonItem = _smay; 

然後,如果你要到別的地方更改標題在你的代碼:

[_smay setTitle:@"Your Other Title"]; 

IOS 8和更高

UIBarButtonItemStyleBordered已棄用,所以請使用下面的內容。

[[UIBarButtonItem alloc] initWithTitle:@"Your Title" 
       style:UIBarButtonItemStylePlain 
       target:self action:@selector(yourMethod)]; 
+0

我做到了。但代碼無法更改標題。 – Rukkora

+0

請將你的UIBarButtonItem初始化代碼發佈到: – hgwhittle

+0

- (void)oped:(NSInteger)newy:(NSInteger)newd – Rukkora

0

如果u想改變導航欄的標題編程意味着

UIBarButtonItem *aDoneItem = [UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered 
    target:self 
    action:@selector(toggleEdit:)]; 

navigationController.rightBarButtonItem = aDoneItem; 

aDoneItem.title = @"Something"; 
相關問題