【问题标题】:Strange crash relating to UITextField.text与 UITextField.text 有关的奇怪崩溃
【发布时间】:2011-04-17 11:59:10
【问题描述】:

我有一个带有 2 个 UITextFields 的视图。我填写了这两个字段并按下一个按钮,该按钮在下面调用此代码。

    userNameStr =[NSString stringWithString:textFieldRounded.text];
clubFanStr =  [NSString stringWithString:clubNameTextField.text];
    NSLog(@"un: %@", userNameStr);
NSLog(@"cb: %@", clubFanStr);

NSLogs 准确地显示了输入到字段中的内容。

然后我按下另一个按钮,出现第三个 UITextField 我将文本放入其中,按下一个按钮并保存,与上面相同。

稍后在应用程序中,当在 NSLog 中使用 clubFanStr 时,应用程序会崩溃。有时,如果 clubFanStr 中包含的任何内容似乎有些有效,它不会使应用程序崩溃。 userNameStr 不会在同一时间使应用崩溃。

所以我虽然把代码改成这个很奇怪

    userNameStr =[NSString stringWithString:clubNameTextField.text];
clubFanStr =  [NSString stringWithString:textFieldRounded.text];
    NSLog(@"un: %@", userNameStr);
NSLog(@"cb: %@", clubFanStr);

基本上交换哪个字符串保存来自哪个 UITextField 的数据。

所以现在当我尝试 NSLog(@"%@",userNameStr); 时,应用程序稍后会崩溃;

所以似乎只有 UITextField clubNameTextField 出现故障。

这是两个声明的代码。

    textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(5, 40, 310, 30)];
textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
textFieldRounded.textColor = [UIColor blackColor]; //text color
textFieldRounded.font = [UIFont systemFontOfSize:17.0];  //font size
textFieldRounded.placeholder = @"User Name?";  //place holder
textFieldRounded.backgroundColor = [UIColor whiteColor]; //background color
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo;   
textFieldRounded.keyboardType = UIKeyboardTypeDefault;  // type of the keyboard
textFieldRounded.returnKeyType = UIReturnKeyDone;  // type of the return key
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing;
textFieldRounded.delegate = self;   
textFieldRounded.hidden = YES;
[self.view addSubview:textFieldRounded];

clubNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, 120, 310, 30)];
clubNameTextField.borderStyle = UITextBorderStyleRoundedRect;
clubNameTextField.textColor = [UIColor blackColor]; //text color
clubNameTextField.font = [UIFont systemFontOfSize:17.0];  //font size
clubNameTextField.placeholder = @"Team you support?";  //place holder
clubNameTextField.backgroundColor = [UIColor whiteColor]; //background color
clubNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;  
clubNameTextField.keyboardType = UIKeyboardTypeDefault;  // type of the keyboard
clubNameTextField.returnKeyType = UIReturnKeyDone;  // type of the return key
clubNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;    
clubNameTextField.delegate = self;  
clubNameTextField.hidden = YES;
//textFieldRounded.editing lets you know of the text field is being currently edited
[self.view addSubview:clubNameTextField];

谁能告诉我我做错了什么?

非常感谢, -代码

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    代替

    userNameStr =[NSString stringWithString:textFieldRounded.text];
    clubFanStr =  [NSString stringWithString:clubNameTextField.text];
    

    试试

    userNameStr = [[NSString alloc] initWithString:textFieldRounded.text];
    clubFanStr =  [[NSString alloc] initWithString:clubNameTextField.text];
    

    【讨论】:

      【解决方案2】:

      clubFanStr 是否用于其他方法?您可能希望保留它,以及 userNameStr。

      【讨论】:

        【解决方案3】:

        稍后在应用程序中,当在 NSLog 中使用 clubFanStr 时,应用程序会崩溃

        这几乎可以肯定意味着您有一个引用计数错误,并且该字符串已被释放。 userNameStrclubFanStr 是如何声明的?您正在为它们分配自动释放的值;它们要么是保留/复制属性,您应该使用self.<propertyName> 来分配,或者它们不是,您应该在分配时保留。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-16
          • 2019-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多