2011-07-18 15 views
0

私はビューバーにプログラムでナビゲーションバーとラベルを作成しました。今私は "完了"ボタンを追加したいが、IBを使用せずに方法を見つけることができないようです....それを行う方法はありますか? ...任意の助けUIBarButtonItemを完全に無料で追加できますか?

おかげ

//Adding navBar programmatically 
CGFloat width = self.view.frame.size.width; 
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,width,52)]; 
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
[self.view addSubview:navBar]; 

//Adding label to navBar programmatically 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,2,width-20,14)]; 
label.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
label.text = @"TITLE"; 
label.backgroundColor = [UIColor clearColor]; 
label.font = [UIFont systemFontOfSize:22]; 
label.shadowOffset = CGSizeMake(1,1); 
label.textColor = [UIColor whiteColor]; 
label.textAlignment = UITextAlignmentCenter; 
[navBar addSubview:label]; 

//Adding back button to navBar programmatically 
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dismissView:)]; 
self.navigationItem.leftBarButtonItem = rightButton; 

最終一部明らかにdoesntの仕事:

は、ここに私のビューコントローラののviewDidLoadです!あなたがあなたのケースでnavigationcontroller

でのViewControllerを押すと

+0

おそらくあなたは[ポスト] [1] :) [1]これを読んsould:http://stackoverflow.com/questions/6464920/buttons-on-navigation-bar-not – Pierre

+0

を-loadingおそらくあなたは[ポスト] [1] :) [1]これを読んsould:http://stackoverflow.com/questions/6464920/buttons-on-navigation-bar-not-loading – Pierre

+0

彼は私にはないuinavigationcontollerを使っています。それは別のケースです。ありがとう。 – TommyG

答えて

1

代わりにUIToolBarを使用していないのはなぜですか?

ボタンをUINavigationBarに追加するには、UINavigationItemを作成する必要があります。 の一部としてUINavigationBarを使用している場合(これは一般的なケースです)、これは自動的に実行され、スタンドアロンビューとして、これを自分で行う必要があります。

UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:@""] autorelease]; 
item.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissView:)] autorelease]; 
[navBar setItems:[NSArray arrayWithObject:item] animated:NO]; 
+0

ありがとうございました!ツールバーについて同意します - なぜUINavigationBarはそこにありますか?とにかくそれを自動的に追加するuinavcontrollerでのみ使うとしたら...?再度、感謝します! – TommyG

関連する問題