开发者

"addTarget:self action:@selector" :: NSArray works, but pointers fail?

"addTarget:self action:@selector" :: NSArray works, but pointers fail on the callBack routine? But calling a routine from viewDidLoad works for both NSArray and pointers? What is different about "addTarget:self action:@selector" callBack??

here is a code snippet:

thanks for 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*));
    for (int i=0; i<10; i++) {
        p_nssB[i]=[NSString stringWithFormat:@"%d", i];
    }

    [self viewMenu];
}


- (void) viewMenu {    
    UIButton *ui开发者_高级运维btnMenu = [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 you allocate with [NSString stringWithFormat:] the instance is autoreleased. It will be released by the next event cycle. You'll need to retain the instance.

So either do:

[[NSString stringWithFormat:@"%d", i] retain];

or:

[[NSString alloc] initWithFormat:@"%d", i];

Also, when you release the file.m object you'll need to do:

for (int i=0; i<10; i++) {
    [p_nssB[i] release];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜