How do you use event handlers with parameters?

I'm trying to figure out how events work. In Stanford's 4 year video on iTunes U, Alan Cannistroro says there is

"3 different tastes of the type of choice of the method of action - (void) actionMethod; - (void) actionMethod: (id) sender; - (void) actionMethod: (id) sender withEvent: (UIEvent *) event;"

I thought I would try them with a simple add program - you enter 2 numbers, and as you enter the amount, it gets places in the 3rd UITextField. I have a method with a signature without parameters, and it works fine. When I change it to the second one with the sender parameter, the code stops calling the method.

It 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 executes, but setSum is not 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 other types of event methods work? Only the first seems to apply.

TIA

Mark

+4
source share
1 answer

A very old question, I know ...

I just tested it:

 - (void) onField { NSLog(@"onField"); } - (void) onField:(id) sender { NSLog(@"onField: %@", sender); } - (void) onField:(id) sender event:(id)event { NSLog(@"onField: %@\n%@", sender, event); } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UITextField *field = [[[UITextField alloc] initWithFrame:CGRectMake(0, 100, 100, 100)] autorelease]; [field setText:@"text"]; [field addTarget:self action:@selector(onField) forControlEvents:UIControlEventEditingChanged]; [field addTarget:self action:@selector(onField:) forControlEvents:UIControlEventEditingChanged]; [field addTarget:self action:@selector(onField:event:) forControlEvents:UIControlEventEditingChanged]; [window addSubview:field]; [window makeKeyAndVisible]; return YES; } 

Results:

 2010-11-04 11:35:59.940 Untitled[599:207] onField 2010-11-04 11:35:59.941 Untitled[599:207] onField: <UITextField: 0x6a0c1f0; frame = (0 100; 100 100); text = 'textsfr'; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a0c350>> 2010-11-04 11:35:59.944 Untitled[599:207] onField: <UITextField: 0x6a0c1f0; frame = (0 100; 100 100); text = 'textsfr'; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x6a0c350>> (null) 

And so it works. The problem is elsewhere.

+2
source

All Articles