ios – Swift中如何应用ChangeCharactersInRange?

栏目: Swift · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/25621496/how-shouldchangecharactersinrange-works-in-swift

我正在使用shouldChangeCharactersInRange作为使用即时类型搜索的一种方式.

但是我有一个问题,应该在文本字段实际更新之前调用shouldChangeCharactersInRange:

在目标C中,我使用下面的方法解决了这个问题:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];

    return YES;
}

不过,我试过在Swift写这篇文章:

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
    let txtAfterUpdate:NSString = self.projectSearchTxtFld.text as NSString
    txtAfterUpdate.stringByReplacingCharactersInRange(range, withString: string)

    self.callMyMethod(txtAfterUpdate)
    return true
}

在获取值之前,该方法仍然被调用?

stringByReplacingCharactersInRange返回一个新的字符串,所以如何:
func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
    var txtAfterUpdate:NSString = self.projectSearchTxtFld.text as NSString
    txtAfterUpdate = txtAfterUpdate.stringByReplacingCharactersInRange(range, withString: string)

    self.callMyMethod(txtAfterUpdate)
    return true
}

http://stackoverflow.com/questions/25621496/how-shouldchangecharactersinrange-works-in-swift


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learning PHP, MySQL, JavaScript, and CSS

Learning PHP, MySQL, JavaScript, and CSS

Robin Nixon / O'Reilly Media / 2012-9-3 / USD 39.99

If you're familiar with HTML, you can quickly learn how to build interactive, data-driven websites with the powerful combination of PHP, MySQL, and JavaScript - the top technologies for creating moder......一起来看看 《Learning PHP, MySQL, JavaScript, and CSS》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试