0

ルートでもあるUINavigationControllerのUIToolbarにボタンを追加する必要があります。特定のUIViewControllerが表示されたときにUIToolbarを表示したい。したがって、このコードをUIViewControllerサブクラスのviewDidLoadメソッドに配置しました。

UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething)];
item.width = 300;
item.tintColor = [UIColor whiteColor];
UIBarButtonItem* item2 = [[UIBarButtonItem alloc] initWithTitle:@"Title2" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething)];

NSMutableArray* theItems = [self.navigationController.toolbar.items mutableCopy];
[theItems addObject:item];
[theItems addObject:item2];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
[self.navigationController setToolbarHidden:NO animated:YES];
[self.navigationController setToolbarItems:theItems animated:NO];
//self.navigationController.toolbarItems = theItems; // Tried both

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
label.text = @"kjhdkjhadsda";
[self.navigationController.toolbar addSubview:label];

これは、UILabelが正しい位置にあることを示すだけで、他には何も表示されません。UILabelは私には役に立たず、単なるテストです。また、コンポーネントから配列をコピーする代わりに、新しい配列を割り当てようとしました。欠落しているリリースは無視してください。これはテストコードのみです。

私はこれについて多くの質問を読みましたが、それを機能させるのに役立つ答えはないようです。このコードで何がうまくいかないかもしれないという考えはありますか?

4

1 に答える 1

2

nilの可変コピーを行に作成しようとしているようです

[self.navigationController.toolbar.items mutableCopy];

デフォルトでは、メソッドnavigationController.toolbar.itemsはアイテムを生成せず、nilを返します。

アップデート

メソッド-(void)setToolbarItems:(NSArray *)toolbarItems Animation:(BOOL)animatedは、UINavigationControllerに送信しても何もしません。ナビゲーションコントローラーによって管理されているコントローラーにツールバー項目を設定する必要があります。この行により、ボタンが表示されます。

[self setToolbarItems:theItems animated:NO];
于 2013-03-06T12:05:59.430 に答える