【发布时间】: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