2012-03-02 11 views
3

UINavigationControllerのツールバーにカスタムラベルを追加しようとしています。私はこのquestionのために一番上の答えに従ったが、それは私のために働くようには見えないし、なぜか分からない。カスタムテキストは表示されませんが、ボタンはそこにあります。それを押すとハイライト表示されますが、テキストはありません。ツールバーでカスタムラベルを追加できない

ここに私のコードです:

- (void) editToolbar{ 
    NSArray *toolbarItemsCopy = [self.toolbarItems copy]; 

    //set up the label 
    self.notificationsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width/0.25, 21.0f)]; 
    self.notificationsLabel.font = [UIFont fontWithName:@"Helvetica" size:20]; 
    self.notificationsLabel.backgroundColor = [UIColor clearColor]; 
    //self.notificationsLabel.textColor = [UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0]; 
    self.notificationsLabel.textColor = [UIColor blueColor]; 
    self.notificationsLabel.text = @"Checking server"; 
    self.notificationsLabel.textAlignment = UITextAlignmentCenter; 

    //init a button with custom view using the label 
    UIBarButtonItem *notif = [[UIBarButtonItem alloc] initWithCustomView:self.notificationsLabel]; 

    //add to the toolbarItems 
    NSMutableArray *toolbarItemsMutableArray = [[NSMutableArray alloc]init]; 

    NSLog(@"toolbar %i", self.toolbarItems.count); 

    //have to add the custom bar button item in a certain place in the toolbar, it should be the 3rd item 
    for (int i = 0; i < toolbarItemsCopy.count; i++) { 
     if (i == 2){     
      [toolbarItemsMutableArray addObject:notif]; 
     } 
     NSLog(@"toolbar item %i %@", i,[toolbarItemsCopy objectAtIndex:i]); 
     [toolbarItemsMutableArray addObject:[toolbarItemsCopy objectAtIndex:i]]; 
    } 
    if (toolbarItemsCopy.count == 4){ 
    }else{ 
     //remove the previous custom label 
     [toolbarItemsMutableArray removeObjectAtIndex:3]; 
    } 
    self.toolbarItems = toolbarItemsMutableArray; 
    //[self.navigationController.toolbar setItems: toolbarItemsMutableArray animated:YES]; 

    NSLog(@"toolbar %i", self.toolbarItems.count); 
}https://stackoverflow.com/questions/ask 

これはのNSLogが印刷されたものである。

Catalogue[361:10403] toolbar 4 
Catalogue[361:10403] toolbar item 0 <UIBarButtonItem: 0x8a24650> 
Catalogue[361:10403] toolbar item 1 <UIBarButtonItem: 0x8a36cd0> 
Catalogue[361:10403] toolbar item 2 <UIBarButtonItem: 0x8a36d30> 
Catalogue[361:10403] toolbar item 3 <UIBarButtonItem: 0x8a382f0> 
Catalogue[361:10403] toolbar 5 

をこれは私がそれを解決する方法である:

を私がALLOCしており、内部でUILabelを初期化関数ではなく、ivarを使用します。

UILabel *notificationsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)]; 
    notificationsLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; 
    notificationsLabel.backgroundColor = [UIColor clearColor]; 
    //self.notificationsLabel.textColor = [UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0]; 
    notificationsLabel.textColor = [UIColor whiteColor]; 
    notificationsLabel.text = @"Checking server"; 
    notificationsLabel.textAlignment = UITextAlignmentCenter; 
+0

NSLogは印刷されていますか? – wizH

+0

@wizHはい彼らはそうです。そして、私はボタンをタップすることができます、テキストだけです。 :) – acecapades

答えて

3

このコードが役立つと確信しています。このコードを念頭に置いてコードを変更してください。あなたはこれらのリンクをたどることができ、より良い理解のために、

UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; 
[self.view addSubview:toolBar]; 

UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f,  self.view.frame.size.width, 21.0f)]; 
[titleLbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]]; 
[titleLbl setBackgroundColor:[UIColor clearColor]]; 
[titleLbl setTextColor=[UIColor blueColor]; 
[titleLbl setText:@"Title"]; 
[titleLbl setTextAlignment:UITextAlignmentCenter]; 

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:titleLbl]; 
NSArray *toolbarItemArr = [NSArray arrayWithObjects:button, nil]; 
[toolBar setItems:toolbarItemArr animated:YES]; 

を、私はこの上で働いている、それが正常に動作している: UIToolBar Class ReferenceUIBarButtonItem Class Reference

+0

もちろん。私はこれを試してみるつもりです。ただ、既存のツールバー項目を編集したいだけです。alloc-initではなく、新しい項目を追加します。しかし、私はこれを試してみましょう。 – acecapades

+0

こんにちは@acecapadesこれは、すべてのロジックが同じになるように、ツールバーアイテム配列のオブジェクトを削除して新しいオブジェクトを割り当てようとしているため、機能します。しかし、コード内にこのコードを慎重に実装する必要があります。私はあなたの答えを見つけることができることを願っています。最高の運勢! –

+0

こんにちは@acecapades私はあなたが私の答えを受け入れるためのあなたの答えを持っていると思う..! –

2

私はこの質問には答えて古いですけど、私は同じ経験をしました興味深いものを見つけました。私のコードは、プログラムの状態に応じて(ツールバーに)ボタンを表示する必要があり、ラベルを表示する必要があります。私は私のコントローラクラスでUILabelオブジェクトを作成し、

UIBarButtonItem *myItem = [[UIBarButtonItem alloc] initWithCustomView: _myUILabel]; 
[self setToolbarItems: [NSArray arrayWithObject: myItem]]; 
[myItem release]; 

への呼び出しで、私はラベルにあるボタンから切り替える必要があるたびに、それを再利用する場合は、ラベルが(実際にそれが現れるまで表示されません最初に、そして一度前後に切り替えた後、それは表示を停止する)。私はこの問題に数時間を費やしていました。もし私が大げさになっていないのであれば、とにかく私のすべての髪を絶望的に引き出していたでしょう。私はすべての保持カウントが正常であることを確認しました。保存されたラベルは有効で有効です。

ツールバーに表示する必要があるたびに新しいラベルを作成すると、問題がなくなることがわかりました。 setToolbarItems内のUILabelの処理には問題があります。だから、私のアドバイスは、新しいラベルを作成し、テキストなどを設定し、それをUIToolBarItemにラップして、ツールバー項目を再度設定します。

これは理想的ではありませんが、allocとreleaseをさらに実行しますが、機能します。私はこれが誰かを助けることを願っています。