views:

41

answers:

1

I'm trying to sort out how the events work. In video 4 of the Stanford course on iTunes U, Alan Cannistraro says there are

"3 different flavors of action method selector types - (void)actionMethod; - (void)actionMethod:(id)sender; - (void)actionMethod:(id)sender withEvent:(UIEvent *)event;"

I thought I'd try them out with a simple adding program - you enter 2 numbers and as you type the sum gets places in a 3rd UITextField. I have a method with the no-parameter signature, and it works fine. When I change it to the second with the sender parameter, the code stops calling the method.

This works:

-(void) setSum {
float a1 = addend1.text.floatValue;
float a2 = addend2.text.floatValue;
float thesum = a1 + a2;
NSString * ssum = [NSString stringWithFormat:@"%g", thesum]; 
sum.text = ssum;
}

-(void)awakeFromNib {
SEL setSumMethod = @selector(setSum);
[addend1 addTarget: self action: setSumMethod forControlEvents: UIControlEventEditingChanged];
[addend2 addTarget: self action: setSumMethod forControlEvents: UIControlEventEditingChanged];
}// awakeFromNib

This runs but setSum doesn't get called:

-(void) setSum:(id) sender {
float a1 = addend1.text.floatValue;
float a2 = addend2.text.floatValue;
float thesum = a1 + a2;
NSString * ssum = [NSString stringWithFormat:@"%g", thesum]; 
sum.text = ssum;
}


-(void)awakeFromNib {
SEL setSumMethod = @selector(setSum:);
[addend1 addTarget: self action: setSumMethod forControlEvents: UIControlEventEditingChanged];
[addend2 addTarget: self action: setSumMethod forControlEvents: UIControlEventEditingChanged];
}// awakeFromNib

So, the question is when do the other event method types work? Only the first seems to apply.

TIA

Mark