【问题标题】:'Search' label text with UITextField带有 UITextField 的“搜索”标签文本
【发布时间】:2012-08-31 20:10:11
【问题描述】:

我有一个 UILabel 和一个 UITextField。每次用户在字段中输入文本时,我都需要检查标签文本中是否存在文本字段文本(基本上我正在搜索标签的文本)。我正在使用

NSRange range = [sentenceRequestLabel.text rangeOfString:resultString];
if (range.location == NSNotFound) {
    NSLog(@"string was found");
} else {
    NSLog(@"string was not found");
}

检查(resultString 是 textfield.text),但即使文本字段中的标签文本甚至不接近标签的文本 NSLog 也会说“找到字符串”。有什么想法吗?

【问题讨论】:

    标签: ios uitextfield uilabel


    【解决方案1】:
    if (range.location == NSNotFound) {
        NSLog(@"string was found");
    

    表示“如果未找到字符串,则打印已找到”。 == 应该是 !=

    【讨论】:

    • 这是我最初的反应,但当我将其更改为 '!=' 时,现在无论文本是什么,它每次都会打印“未找到字符串”。
    • @JohnWickham 能否请您发布更多带有上下文的代码?
    • 当然...附加到文本字段的 ValueDidChange 操作,我有:
    • -(void)evaluateString { resultString = blindfield.text; NSRange 范围 = [sentenceRequestLabel.text rangeOfString:resultString]; if (range.location != NSNotFound) { NSLog(@"找到字符串"); } else { NSLog(@"找不到字符串"); } }
    • @JohnWickham 在这种情况下你确定blindfieldblindfield.text 不为零吗?
    猜你喜欢
    • 2016-12-06
    • 2016-09-25
    • 1970-01-01
    • 2010-11-24
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多