1

ツールバーにボタンを追加するのに最適なコードがあります。

NSArray* toolbarItems = [NSArray arrayWithObjects:flexibleSpace,shuffleBarItem,flexibleSpace,nil];
self.toolbarItems = toolbarItems;

ただし、ツールバー項目も削除できるようにしたいです。以下の方法を使用すると、アプリケーションがクラッシュします。

NSArray* toolbarItems = [NSArray arrayWithObjects:flexibleSpace,nil];
self.toolbarItems = toolbarItems;

iPhoneのツールバーを動的に変更する方法を知っている人はいますか?

ありがとう!

4

2 に答える 2

1

に変更しNSMutableArrayます。

NSMutableArray* _toolbarItems = [NSMutableArray arrayWithCapacity: 3]; 
[ _toolbarItems addObjects: flexibleSpace,shuffleBarItem,flexibleSpace,nil];

self.toolbarItems = _toolbarItems;

配列からアイテムを削除する場合:

NSInteger indexOfItem = ...
[ _toolbarItems removeObjectAtIndex: indexOfItem ];

self.toolbarItems = _toolbarItems;

この場合removeObject、配列内に繰り返しオブジェクトがあるため、使用しないでください。呼び出す[ _toolbarItems removeObject: flexibleSpace ]と、実際flexibleSpaceには配列内の両方のインスタンスが削除されます。

于 2010-04-11T20:20:56.807 に答える
1

前面または背面からアイテムを削除するには、次のように使用できますsubarrayWithRange

NSRange allExceptLast;
allExceptLast.location = 0;
allExceptLast.length = [self.toolbarItems count] - 1;
self.toolbarItems = [self.toolbarItems subarrayWithRange:allExceptLast];

途中からオブジェクトを削除したい場合は、-[NSArray filteredArrayUsingPredicate:](非常に複雑になる可能性があります)、またはブルートフォースを使用できます。

NSMutableArray *mutToolbarItems = [NSMutableArray arrayWithArray:self.toolbarItems];
[mutToolbarItems removeObjectAtIndex:<index of object>];
self.toolbarItems = mutToolbarItems;

removeObjectAtIndex:に直接送信しないでください(上記の方法を使用しself.toolbarItemsた場合でも) 。舞台裏 として実装されています)。toolbarItemsNSArrayNSMutableArray

于 2010-04-11T22:53:20.833 に答える