所以我有以下代码行:
text = [text stringByReplacingOccurrencesOfString:@"%@" withString:[NSString stringWithString:name]];
[NSString stringWithString:name]
防止单个字符串异常。
但是,现在我的问题是,如果有人输入了一个name
以上的单词,即name = @"John Doe";
,则整行都会失败。我觉得这是一个非常简单的解决方法,但对于我的生活来说,我无法弄清楚。有谁知道怎么回事?
编辑:包括错误行周围的代码。
此函数存储名称:
- (void) buttonSelected:(UIButton *)button
{
NSString *text;
if ([button.titleLabel.text isEqualToString:@"NEXT"] || [button.titleLabel.text isEqualToString:@"DEBATE BACK"] || [button.titleLabel.text isEqualToString:@"ISSUE A CLOTURE"])
{
bool noText = true;
bool foundTextBox = false;
for (UIView *view in [scrollView subviews])
{
if (view.tag == 51)
{
if ([view isKindOfClass:[UITextField class]])
{
foundTextBox = true;
UITextField *blah = (UITextField *)view;
text = blah.text;
NSLog(@"%@", text);
if (![text isEqualToString:@""] && text != nil) noText = false;
}
}
}
if (!foundTextBox) noText = false;
if (!noText)
{
if (questionNumber == 0) name = text;
else if (questionNumber == 1) state = text;
else if (questionNumber == 2) billSubject = text;
[self hideKeyboard];
[UIView animateWithDuration:0.8 animations:^
{
for (UIView *view in [scrollView subviews])
{
CGPoint origin = CGPointMake(-10 - (view.frame.size.width/2), view.frame.origin.y+(view.frame.size.height/2));
[view setTag:view.tag + 100];
[view setCenter:origin];
[view setAlpha:0.0];
}
}
completion:^(BOOL finished)
{
for (UIView *view in [scrollView subviews])
{
[view removeFromSuperview];
}
[self nextQuestion];
}];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!" message:@"You have not entered any text!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show]; [alert release];
}
}
}
现在我想起来了,由于NSLog(@"%@", text);
某种原因,从未真正打印过。甚至没有空行。但是,noText
在查找文本框的 for 循环之后仍然为 false;
这里是调用name
:
- (void) setText:(NSString *) text label:(UILabel *) label
{
if (questionNumber == 1)
{
NSLog(@"%@", name);
text = [text stringByReplacingOccurrencesOfString:@"%@" withString:[NSString stringWithString:name]];
label.text = text;
}
else
label.text = text;
}
name
确实打印了整个名称。
注意:questionNumber
、name
、state
、 和billSubject
都是全局变量。