在beginEditing上选择UISearchBar中的所有文本吗? [英] Select all text in UISearchBar on beginEditing?

查看:70
本文介绍了在beginEditing上选择UISearchBar中的所有文本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模仿iOS Safari中地址栏的行为-也就是说,如果在点击时地址栏中有文本,则将其选中.大多数台式机浏览器也以相同的方式工作.我不想在选择时清除它,而只是选择里面的内容,这样,如果用户随后开始键入内容,它将被清除,但用户也可以根据需要编辑现有字符串.

I’m trying to emulate the behaviour of the address bar in iOS Safari – that is, if there’s text in the address bar when it’s tapped, it is selected. Much in the same way most desktop browsers work, too. I don’t want to CLEAR it on selection, merely select what’s inside, so that if the user then begins typing, it’ll clear, but the user also has the ability to edit the existing string if they want to.

我尝试了以下操作,它在iOS7和iOS6下的子视图中都找到了 UISearchBarTextField ,但是对其调用 selectAll 似乎没有任何效果.我尝试更改其 text 属性,效果很好,只是 selectAll 不满意.

I’ve tried the following, it finds the UISearchBarTextField inside the subviews under both iOS7 and iOS6, but calling selectAll on it doesn’t seem to have any effect. I’ve tried changing its text property, that works fine, it’s just selectAll that isn’t happy.

我还尝试将参数更改为 selectAll:更改为 self ,还有其他几件事,没有任何变化,只是不想在其中选择文本!

I’ve also tried changing the argument to selectAll: to self and a couple other things, no change, just doesn’t wanna select the text in there!

有什么想法吗?这是我的代码:

Any thoughts? Here’s my code:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    NSArray *subviews = [UIDevice isRunningiOS7OrAbove]
    ? [[searchBar.subviews objectAtIndex:0] subviews]
    : searchBar.subviews;

    for (UIView *subview in subviews) {
        if ([subview conformsToProtocol:@protocol(UITextInputTraits)]) {
            UITextField *textField = (UITextField *)subview;
            [textField selectAll:textField];
        }
    }
}

编辑

我也尝试过这个,没有高兴:

I’ve also tried this, with no joy:

for (UIView *subview in subviews) {
    if ([subview conformsToProtocol:@protocol(UITextInputTraits)]) {
        UITextField *textField = (UITextField *)subview;
        [textField setSelectedTextRange:[textField textRangeFromPosition:[textField beginningOfDocument]
                                                              toPosition:[textField endOfDocument]]];
    }
}

推荐答案

遇到同样的问题.帮助我的事情是将 selectAll 延迟了一小段时间.

Ran into the same problem. The thing that helped me was delaying the selectAll by a short period of time.

我为 UISearchBar

extension UISearchBar {
    func selectAllText() {
        guard let searchTF = self.valueForKey("searchField") as? UITextField else {
            return
        }

        if searchTF.respondsToSelector(#selector(NSObject.selectAll(_:))) {
            searchTF.selectAll(nil)
        }
    }
}

然后,我会在搜索栏中短暂地调用此方法.

then I simply call this method on my searchbar with a short delay.

雨燕2

let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.05 * Double(NSEC_PER_SEC))) 
dispatch_after(dispatchTime, dispatch_get_main_queue(), { [weak self] in
    self?.nameSearchBar.selectAllText()
})

Swift 3 Xcode 8 beta 4

Swift 3 Xcode 8 beta 4

DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self] in
    self?.nameSearchBar.selectAllText()
}

警告:这种方法远非明确的解决方案,但效果很好.短暂的延迟不会造成任何麻烦,因为它只是对文本字段的选择操作.尽管访问 UISearchBar searchField 属性可能会导致应用被拒绝!

WARNING: This approach is far from clear solution, but it works fine. The short delay shouldn't cause any trouble since it's just a selecting action on a textfield. Though accessing the searchField property of your UISearchBar can lead to app rejection!

这篇关于在beginEditing上选择UISearchBar中的所有文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆