2013-08-21 12 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がnilかどうかを確認しましたか? –

+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"; 
関連する問題