Strange crash relating to UITextField.text
I've got a view with 2 UITextFields on it. I fill in both fields and press a button which calls this code below.
userNameStr =[NSString stringWithString:textFieldRounded.text];
clubFanStr = [NSString stringWithString:clubNameTextField.text];
NSLog(@"un: %@", userNameStr);
NSLog(@"cb: %@", clubFanStr);
The NSLogs show exactly what was typed into the fields.
I then press another button, a 3rd UITextField appears I put text in that, press a button and it saves, same as above.
Later on in the app when clubFanStr is used in an NSLog it crashes the app. Sometimes it doesnt crash the app if whatever is contained in clubFanStr seems to be somewhat valid. userNameStr never crashes the app at the same point.
So I though this was strange at changed the code to this
userNameStr =[NSString stringWithString:clubNameTextField.text];
clubFanStr = [NSString stringWithString:textFieldRounded.text];
NSLog(@"un: %@", userNameStr);
NSLog(@"cb: %@", clubFanStr);
Basically swapping which string holds data from which UITextField.
So now app crashes later on when i try NSLog(@"%@",userNameStr);
So it seems that only the UITextField clubNameTextField is malfunctioning.
Here is code of both of there declarations.
textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(5, 40, 310, 30)];
textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
textFieldRounded.textColor = [UIColor开发者_如何学JAVA 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];
Anybody able to clue me into what I've done wrong?
Many Thanks, -Code
Instead of
userNameStr =[NSString stringWithString:textFieldRounded.text];
clubFanStr = [NSString stringWithString:clubNameTextField.text];
try
userNameStr = [[NSString alloc] initWithString:textFieldRounded.text];
clubFanStr = [[NSString alloc] initWithString:clubNameTextField.text];
Is clubFanStr used in another method? You may want to retain it then, and the userNameStr as well.
Later on in the app when clubFanStr is used in an NSLog it crashes the app
This almost certainly means that you have a reference counting error, and the string has been deallocated. How are userNameStr
and clubFanStr
declared? You are assigning autoreleased values to them; either they are retain/copy properties, and you should be using self.<propertyName>
to assign to, or they aren't and you should be retaining as you assign.
精彩评论