iphone - NSString 超出范围问题?

标签 iphone objective-c xcode

我真的很抓狂,这一定是一个简单的问题,但我就是看不到。

我只是尝试从文本字段为变量分配一个值。

在我的 h 文件中,我有

NSString *currentPass; 
@property (nonatomic, retain) NSString *currentPass;

我的 m 文件。

@synthesize currentPass;
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:    
    (NSInteger)buttonIndex{

if (alertView.tag == AlertPasswordAsk) {
    UITextField* theTextField = ((UITextField*)[alertView viewWithTag: 5]);
    currentPass = [NSString stringWithFormat:@"%@", theTextField.text];
    if ([theTextField isEditing]) {
        [theTextField resignFirstResponder];
    } 
}
}

- (void)alertView:(UIAlertView *)alertView 
    didDismissWithButtonIndex:(NSInteger)buttonIndex{

    NSLog(@"didDismissWithButtonIndex tag=%i", alertView.tag);
if (alertView.tag == AlertPasswordAsk) {
    if(buttonIndex == 1){
        NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
        NSString *strPassword = [NSString alloc];
        strPassword = [myDefaults stringForKey:@"pass"];

             // #########  ERROR OCCURS HERE #########
        NSLog(@"currentPass=%@ strPassword=%@", currentPass, strPassword);

        if (![currentPass isEqualToString:strPassword]) {

alt text

[6337:207] didDismissWithButtonIndex tag=10
Current language:  auto; currently objective-c
(gdb) continue
Program received signal:  “EXC_BAD_ACCESS”.
(gdb) bt
#0  0x02894903 in objc_msgSend ()
#1  0x00000000 in ?? ()

最佳答案

您需要保留分配给currentPass的对象:

self.currentPass = [NSString stringWithFormat:@"%@", theTextField.text];

关于iphone - NSString 超出范围问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4184323/

相关文章:

objective-c - 将 UIDatePicker 装入 UIActionSheet

ios - 如何从iOS应用的今日扩展中设置角标(Badge)计数?

ios - ios 中的 CrashReporter 符号化客户端

xcode - 在cocos2d2.x上的cocos3d 3.1。重命名层导致崩溃

iphone - 获取 Facebook 应用程序订阅者

iphone - 局部声明隐藏实例变量 - tableView 未合成

加载 Storyboard IB 时,Xcode 7.3 非常慢

xcode - 安装pod时是否需要关闭Xcode项目?

iphone - 隐式转换将 64 位缩短为 32 位

ios - GCD 等待 NSURLSessionDataTask 完成