是否可以阻止用户在键入时在搜索栏上输入某些字符。例如,用户键入一些字符串... "This is a text "
,在单词文本后,用户尝试输入'*'
字符。我希望阻止用户输入该字符。
是否可以限制用户在搜索栏上输入时输入某个字符?
答案 0 :(得分:2)
UISearchBar
的代理与UITextField
的代理非常相似。实现委托的searchBar:shouldChangeTextInRange:replacementText:
方法。根据需要返回YES
或NO
。如果您返回NO
,则不会输入文字。
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
// check the resulting text. Return NO if not allowed
}