2010-12-18 14 views
5

私は最近、UIToolbarsに関する質問をしていましたが、それはプログラム的に項目を追加する必要があることを発見しました。私はそれをやり遂げる方法について他の人の方法を見てきました。しかし、私が同じことをやろうとすると、何も現れません。この問題を特定することは、私が助けが必要なことです。ここでIBの私の接続です:プログラムでUIToolbarに項目を追加する

ヘッダファイル:

#import <UIKit/UIKit.h> 

@interface ParkingRootViewController : UIViewController { 
    UINavigationController *navigationController; 
    UIToolbar *toolbar; 
    UIBarButtonItem *lastUpdateLabel; 
} 

@property(nonatomic, retain) IBOutlet UINavigationController *navigationController; 
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar; 
@property (nonatomic, retain) IBOutlet UIBarButtonItem *lastUpdateLabel; 

- (IBAction)selectHome:(id)sender; 

@end 

実装ファイル:

- (void)viewDidLoad { 
     [super viewDidLoad]; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)]; 
    label.text = @"last updated..."; 
    label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.textAlignment = UITextAlignmentCenter; 
    label.font = [UIFont boldSystemFontOfSize:13.0]; 
    label.userInteractionEnabled = NO; 

    lastUpdateLabel = [[UIBarButtonItem alloc] initWithCustomView:label]; 
    [label release]; 
    [toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]]; 


    [self.view addSubview:self.navigationController.view]; 
    //[self.view addSubview:toolbar]; 
    //[self.navigationController.view addSubview:toolbar]; 

    [self.navigationController.view setFrame:self.view.frame]; 

} 

すべてのヘルプれ

alt text

そしてここでは、関連するコードです非常に感謝!

EDIT:

私はツールバーには、次のように変更し、私はのviewDidLoadで自分のコードを更新する/表示される場合がありますペン先に持っていたものは何でも削除:

self.navigationController.toolbarHidden = NO; 

    //creating label in tool bar 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)]; 
    label.text = @"last updated..."; 
    label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.textAlignment = UITextAlignmentCenter; 
    //label.highlightedTextColor = [UIColor colorWithWhite:0.5 alpha:1.0]; 
    //label.highlighted = YES; 
    label.font = [UIFont systemFontOfSize:13.0]; 
    label.userInteractionEnabled = NO; 

    UIBarButtonItem *lastUpdateLabel = [[UIBarButtonItem alloc] initWithCustomView:label]; 
    //[lastUpdateLabel initWithCustomView:label]; 
    //[label release]; 
    //[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]]; 
    [self setToolbarItems:[NSArray arrayWithObject:lastUpdateLabel]]; 

そして、私は終わります空のツールバーが表示されます。私はデバッガを起動し、これが表示されます。 enter image description here Aha! lastUpdateLabelのビューの_textフィールドが範囲外です!しかし、なぜ?そして私はこれをどのように救済するのだろうか?前もって感謝します!

EDIT 2:

私はラベルを追加することができたし、次のコードでNSActivityIndi​​catorています

@synthesize refreshDataButton; 
//... 
self.navigationController.toolbarHidden = NO; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 80.0f, 40.0f)]; 
    label.text = @"last updated..."; 
    label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.textAlignment = UITextAlignmentCenter; 
    label.font = [UIFont systemFontOfSize:13.0]; 
    label.userInteractionEnabled = NO; 
    [self.toolbar addSubview:label]; 

// create activity indicator 
    //      dist frm lft, dist frm top 
    CGRect frame = CGRectMake( 90.0,   11.0,  25.0, 25.0);  
    UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithFrame:frame]; 
    loading.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; 
    [loading sizeToFit];  
    loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
           UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | 
           UIViewAutoresizingFlexibleBottomMargin);  
    [loading startAnimating]; 

    [self.toolbar addSubview:loading]; 

しかし、私は、私は運を持っていないUIBarButtonItemを追加しようとする(表示されません。ツールバーのアップ):

#import <UIKit/UIKit.h> 
//#import <CoreData/CoreData.h> 

@interface ParkingRootViewController : UIViewController { 
    UINavigationController *navigationController; 
    UIToolbar *toolbar; 
    UIBarButtonItem *refreshDataButton; 
    //UIActivityIndicatorView *loading; 
} 

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController; 
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar; 
@property (nonatomic, retain) UIBarButtonItem *refreshDataButton; 
//@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *loading; 


@property (nonatomic, readonly) NSString *applicationDocumentsDirectory; 

-(IBAction)selectHome:(id)sender; 
-(void)testCoreData; 
-(void)refreshDataButtonTapped; 

@end 
:ここ

self.refreshDataButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(refreshDataButtonTapped)]; 
[self setToolbarItems:[NSArray arrayWithObject:refreshDataButton]]; 

は、ヘッダファイルであります

アイデアありがとう!

答えて

0

投稿されたコードは正常に動作しますが、XIBがどのように接続されているかが必要です。私はIBであなたの接続を再開します(つまり、すべての接続を解除して再作成します)、XIBを保存してもう一度やり直してください。

+0

は、あなたが示唆したものを運がなかったです。私はただの灰色のツールバーが表示されます。このコード行で投げます:toolbar.barStyle = UIBarStyleBlackOpaque;実際にはツールバーが黒くなるので、接続がうまくいけません。 – Stunner

12

コードは機能するはずです...私が作ることができるいくつかの提案があります。代わりに:、また

[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel] animated:YES]; 

をおUINavigationControllerを使用しているので、NavControllersは、あなたが好きなら、あなたが使用することができ、独自のツールバーが付属しています:

[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]]; 

これを試してみてください。

self.navigationController.toolbarHidden = NO; 

、あなたがこれを行うことによって、そのツールバー項目を設定することができます:デフォルトでは、それはあなたがこれを行うことにより、可視化することができた、隠され

[self setToolbarItems:[NSArray arrayWithObject:lastUpdateLabel]]; 

・ホープ、このTIDビットはあなたを助けます。がんばろう!

+0

ありがとう、私はself.navigationController.toolbarHiddenを見つけました= NO;私のために使用/仕事になる。しかし、私はまだ空のツールバーが発生しています。上記の質問に編集した内容をご覧ください。 – Stunner

+2

2つの異なるツールバーを使用しているようです。ナビゲーションツールバーを使用する場合は、 "self.navigationController.toolbar"を使用してそれを言及する必要があります。独自のツールバーを使用している場合は、プロパティとして設定されている場合は、 "self.toolbar"を使用して言及することができます。あなたのサブビューは自分のツールバーに追加されているように見えますが、アイテムはナビゲーションツールバーに設定されています。私は問題を探し始めるでしょう。 – Bittu

0

ボタンは、で押されたviewControllerによって追加する必要があります。 navControllerはバーを保持しますが、バーのアイテムは表示されているviewControllerによって制御(追加)されます。そのように、それぞれの異なる種類のVCには、独自のバーがあります。

そのbarbuttonitemコードをとり、それをvcのinitセクションに入れてお楽しみください。

0

//これを試してください。

- (void)viewDidAppear:(BOOL)animated 
{ 
    [toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]]; 
} 
0

uは、フレームとそれをインスタンス化している場合は、直接ツールバーにラベルを追加することができます...例えば

UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(7,7,200,30)]; 

    [lbl setBackgroundColor:[UIColor clearColor]]; 

    [lbl setText:@"Test lbl"]; 

    [_toolBar addSubview:lbl]; 
関連する問題