objective c - "addTarget:self action:@selector" :: NSArray works, but pointers fail? -
"addtarget:self action:@selector" :: nsarray works, pointers fail on callback routine? calling routine viewdidload works both nsarray , pointers? different "addtarget:self action:@selector" callback??
here code snippet:
thanks looking...
file.h
nsarray *nsarra; nsstring **p_nssb;
file.m
- (void)viewdidload { nsarra = [[nsarray alloc] initwithobjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",nil]; p_nssb = (nsstring**)malloc(10*sizeof(nsstring*)); (int i=0; i<10; i++) { p_nssb[i]=[nsstring stringwithformat:@"%d", i]; } [self viewmenu]; } - (void) viewmenu { uibutton *uibtnmenu = [uibtnmenu addtarget:self action:@selector(actionmenu:) forcontrolevents:uicontroleventtouchupinside]; [uiviewmenu addsubview:uibtnmenu]; nslog(@"nsarra: %@ %@ %@", [nsarra objectatindex:0], [nsarra objectatindex:1], [nsarra objectatindex:2]);//<--works nslog(@"p_nssb: %@ %@ %@", p_nssb[0], p_nssb[1], p_nssb[2]);//<--works } - (void) actionmenu: (uibutton *) uibtnmenu{ nslog(@"nsarra: %@ %@ %@", [nsarra objectatindex:0], [nsarra objectatindex:1], [nsarra objectatindex:2]);//<--works nslog(@"p_nssb: %@ %@ %@", p_nssb[0], p_nssb[1], p_nssb[2]);//<--fails(drops out of app) }
when allocate [nsstring stringwithformat:] instance autoreleased. released next event cycle. you'll need retain instance.
so either do:
[[nsstring stringwithformat:@"%d", i] retain];
or:
[[nsstring alloc] initwithformat:@"%d", i];
also, when release file.m object you'll need do:
for (int i=0; i<10; i++) { [p_nssb[i] release]; }
Comments
Post a Comment