开发者

How do you use event handlers with parameters?

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 a开发者_高级运维2 = 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


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;
}

The 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)

So it just works. The problem is somewhere else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜