内容简介: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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
疯狂HTML 5/CSS 3/JavaScript讲义
李刚 / 电子工业出版社 / 2012-5-1 / 69.00元
疯狂HTML 5/CSS 3/JavaScript讲义,ISBN:9787121168635,作者:李刚 编著一起来看看 《疯狂HTML 5/CSS 3/JavaScript讲义》 这本书的介绍吧!