开发者

Referring to an NSString in output of UIAlertView?

I have an NSString, testString which is set to a value in my app. Then as a response to a button press in UIAlertView I have the following code:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSLog(@"String: %@", testString);
    }
}

But 开发者_如何学Cfor sme reason, this always causes the app to crash. I can't seem to figure out why.


Probably you create your string with one of convenience methods and don't retain it, so when it comes to alert's clickedButtonAtIndex it's already autoreleased. You should define it as

@property (nonatomic, retain) NSString *testString;

In implementation:

@synthesize testString;

On creating:

self.testString = [NSString stringWithFormat:@"%@", @"My test string"];

and [testString release]; on dealloc.

Then when you use it in clickedButtonAtIndex you can be sure your testString has a right value

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜