在UIAlertView的输出中引用NSString? [英] Referring to an NSString in output of UIAlertView?

查看:49
本文介绍了在UIAlertView的输出中引用NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSString testString,它设置为我的应用程序中的一个值。然后,作为对按钮的响应,请在 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 for sme reason, this always causes the app to crash. I can't seem to figure out why.

推荐答案

可能您使用一种便捷方法创建了字符串,但没有保留它,因此当涉及到警报的clickedButtonAtIndex时,它已经自动释放。
您应将其定义为

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;

在实施中:

@synthesize testString;

创建时:

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

[testString release]; 在dealloc上

然后,当在clickedButtonAtIndex中使用它时,可以确保您的testString具有正确的值

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

这篇关于在UIAlertView的输出中引用NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆