objective c - How do you use event handlers with parameters? -
i'm trying sort out how events work. in video 4 of 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 simple adding program - enter 2 numbers , type sum gets places in 3rd uitextfield. have method no-parameter signature, , works fine. when change second sender parameter, code stops calling 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 setsum doesn't 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, question when other event method types work? first seems apply.
tia
mark
very old question, know...
i 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 works. problem somewhere else.
Comments
Post a Comment