Referring to an NSString in output of UIAlertView?

懵懂的女人 提交于 2019-12-25 04:09:04

问题


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 for sme reason, this always causes the app to crash. I can't seem to figure out why.


回答1:


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



来源:https://stackoverflow.com/questions/4841399/referring-to-an-nsstring-in-output-of-uialertview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!