What I’m trying to do is have a single function for each UIButton that is created, and use a switch inside the function to determine its behaviour. I’m setting the tag on the button to use the switch, but it’s not actually getting that far.
for var(int i = 0; i < numResults; i++)
{
UIButton* button = [[UIButton] alloc] initWithFrame:CGRectMake(0,(i*55)+10,320,50)];
[buttton setTag:i];
[button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
...
-(void) buttonHandler:(id)sender
{
//Handle the button press
}
When I click any of the buttons the app throws an error:
-[WatchViewController buttonHandler]: unrecognized selector sent to instance 0x6845430
I believe that this is because the button variable is created, and then as there is no reference to it, autoreleased by ARC, so it no longer exists when the function is called. Unfortunately, I don’t know how to keep a reference to each button in memory.
If I’m wrong (or anything I’ve written is poor practice), feel free to tell me- it’ll help me to learn!
Your problem is in your use of
@selector(buttonHandler). You meant@selector(buttonHandler:). Note the extra colon at the end. I like to turn on the “Undeclared Selector” (-Wselector) warning to catch this kind of mistake.