我正在开发一个动态添加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
这里我的问题是在它的选择器中只有一个参数的条形按钮工作正常(createPlantDetails
)但是当我点击在它的选择器中有两个参数的条形按钮(createInventory
)时,应用程序正在崩溃[button setTintColor:[UIColor redColor]];
的changeSelection
方法。
崩溃日志类似于:touches event have no method like setTintColor .
我搜索了很多,但找不到解决方案。请帮我。
提前致谢