我有一个包含2个UITextFields的视图。我填写两个字段,然后按下一个按钮,调用下面的代码。
userNameStr =[NSString stringWithString:textFieldRounded.text];
clubFanStr = [NSString stringWithString:clubNameTextField.text];
NSLog(@"un: %@", userNameStr);
NSLog(@"cb: %@", clubFanStr);
NSLogs显示了在字段中输入的内容。
然后按下另一个按钮,出现第3个UITextField,我将文本放入其中,按下按钮并保存,与上面相同。
稍后在应用程序中当clubFanStr用于NSLog时,它会崩溃应用程序。如果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];
有人能够让我知道我做错了吗?
非常感谢, -code
答案 0 :(得分:0)
clubFanStr是否用于另一种方法?您可能希望保留它,以及userNameStr。
答案 1 :(得分:0)
稍后在应用程序中,当在NSLog中使用clubFanStr时,它会崩溃应用程序
这几乎肯定意味着您有一个引用计数错误,并且该字符串已被释放。如何声明userNameStr
和clubFanStr
?您正在为它们分配自动释放的值;它们是保留/复制属性,您应该使用self.<propertyName>
分配,或者它们不是,您应该在分配时保留。
答案 2 :(得分:0)
而不是
userNameStr =[NSString stringWithString:textFieldRounded.text];
clubFanStr = [NSString stringWithString:clubNameTextField.text];
试
userNameStr = [[NSString alloc] initWithString:textFieldRounded.text];
clubFanStr = [[NSString alloc] initWithString:clubNameTextField.text];