ツールバーのボタンを実行時に切り替える際の注意
現在作成中のアプリでは、下のようにツールバーに乗っているボタン等を実行時に切り替える必要があります。
- toolbar1
- toolbar2
ツールバー上のアイテム(UIBarButtonItem) はsetToolbarItemsを使って、配列でツールバーに設定できます。
そこで、以下のように toolbar1, toolbar2の配列を作って実行時に、setToolbarItems: に toolbar1、またはtoolbar2を設定する事で切り替えるようにしたところ、なぜか右端のスライダーが表示されません !!
pageSlider = [[UISlider alloc] initWithFrame:CGRectMake(0,0,200,30)]; .... soundSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"●●●●", @"○○○○", nil]]; .... self.toolbar1 = [[NSArray alloc] initWithObjects: BarImageFileButtonItem(@"play_icon.png", @selector(repeatSound)), [[[UIBarButtonItem alloc] initWithCustomView:soundSegment] autorelease], BarFlexibleSpaceButtonItem, [[UIBarButtonItem alloc] initWithCustomView:pageSlider], nil]; self.toolbar2 = [[NSArray alloc] initWithObjects: BarFlexibleSpaceButtonItem, [[UIBarButtonItem alloc] initWithCustomView:pageSlider], nil];
いろいろと試してみたのですが、夜中になって解りました! スライダーは1つのオブジェクト(インスタンス)を使っていますが、それを乗せているUIBarButtonItemが同じオブジェクトでないのが原因でした ^^);
pageSlider = [[UISlider alloc] initWithFrame:CGRectMake(0,0,200,30)]; .... soundSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"●●●●", @"○○○○", nil]]; .... self.toolbar1 = [[NSArray alloc] initWithObjects: BarImageFileButtonItem(@"play_icon.png", @selector(repeatSound)), [[[UIBarButtonItem alloc] initWithCustomView:soundSegment] autorelease], BarFlexibleSpaceButtonItem, [[UIBarButtonItem alloc] initWithCustomView:pageSlider], nil]; self.toolbar2 = [[NSArray alloc] initWithObjects: [toolbar1 objectAtIndex:2], [toolbar1 objectAtIndex:3], nil];
このように変更したところ動きました。