動的に追加UIBarButtonItem
するアプリケーションに取り組んでいます。UIToolbar
ユーザーがバーのボタンをクリックしたとき。色合いを赤に変えています。しかし、一部のバー ボタンでは機能せず、アプリケーションがクラッシュします。
これが私のコードです:
@interface myClass : UIViewController
@property (nonatomic, retain) NSMutableArray *barButtonItems;
@property (nonatomic, retain) IBOutlet UIToolbar *toolBar;
@end
@implementation myClass
@sythesize barButtonItems, toolBar;
- (void)viewDidLoad
{
[super viewDidLoad];
barButtonItems = [[NSMutableArray alloc] init];
[self initToolBar];
}
//To set the tool bar
- (void)initToolBar
{
[self addBarItem:@"PlantDetails" actionName:@"createPlantDetails:"];
[self addBarItem:@"ElectricalEquipmentInventory" actionName:@"createInventory:button:"];
toolBar.items = barButtonItems;
}
//Create bar button item
- (void)addBarItem:(NSString*)barButtonName actionName:(NSString*)methodName
{
UIBarButtonItem *plantDetails = [[UIBarButtonItem alloc] initWithTitle:barButtonName style:UIBarButtonItemStyleDone target:self action:NSSelectorFromString(methodName)];
[barButtonItems addObject:plantDetails];
[plantDetails release];
plantDetails = nil;
}
//Changes the barbutton tintcolor when user selected
-(void)changeSelection:(UIBarButtonItem *)button
{
NSArray *tempArray = toolBar.items;
for(int loop = 0; loop<[tempArray count]; loop++)
[[tempArray objectAtIndex:loop] setTintColor:[UIColor blackColor]];
[button setTintColor:[UIColor redColor]];
}
//First bar button method
- (void)createPlantDetails:(UIBarButtonItem *)button
{
[self changeSelection:button];
NSLog(@"createPlantDetails");
}
//second bar button method
- (void)createInventory:(int)selectedIndex button:(UIBarButtonItem *)button
{
[self changeSelection:button];
NSLog(@"createInventory");
}
@end
ここで私の問題は、セレクターにパラメーターが1つしかないバーボタンが完全に機能していることです( createPlantDetails
)が、セレクターに2つのパラメーターがあるバーボタンをクリックすると( )、アプリケーションがメソッドcreateInventory
でクラッシュします。[button setTintColor:[UIColor redColor]];
changeSelection
クラッシュログは次のようなものです:touches event have no method like setTintColor .
私はたくさん検索しましたが、解決策を見つけることができませんでした。私を助けてください。
前もって感謝します