1

動的に追加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 .

私はたくさん検索しましたが、解決策を見つけることができませんでした。私を助けてください。

前もって感謝します

4

1 に答える 1

2

プロパティのメソッドactionは、次の 3 つの形式のいずれかである必要があります。

- (void)methodName;
- (void)methodName:(id)sender;
- (void)methodName:(id)sender withEvent:(UIEvent *)event;

任意の形式やカスタム パラメータを使用することはできません (ボタンは何を渡せばよいかわかりません)。


このcreatePlantDetails:メソッドは、2 番目の形式に一致するため機能します。


createInventory:button:予想される署名のいずれにも一致しないため、メソッドは失敗します 。
メソッドには 2 つのパラメーターがあるため、ボタンがメソッドを呼び出すと、ボタンはUIEvent2 番目のパラメーターでオブジェクトを渡します。メソッドで button.

では、実際にはであり、送信者ではないためchangeSelection:、呼び出しを試みるとクラッシュします。setTintColor:buttonUIEventUIBarButtonItem

于 2012-11-29T18:45:50.160 に答える