I have aUISearchBar
, I get theUITextfield
in it and then I want to set selected Text range but it's always return nil. This is my code:
UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];
[searchField becomeFirstResponder]; // focus to this searchfield
And I fill in the text:
self.searchBar.text = @"This is text";
And checked if theUITextField
text is filled in and it is. Still, all the methods regardingUITextPosition
andUITextRange
return nil:
UITextRange *selectedRange = [searchField selectedTextRange]; //selectedRange = nil
UITextPosition *newPosition = [searchField positionFromPosition:selectedRange.start offset:addressToComplete.street.length]; //newPosition = nil
UITextRange *newRange = [searchField textRangeFromPosition:newPosition toPosition:newPosition]; //newRange = nil
My code is wrong something?
Have you tried Logging
UITextField *searchField
? its nil from the looks of it.. i think your code is supposed to be..(UITextField *)[self.searchBar valueForKey:@"_searchField"]
([after trying your code] OooOoKayyy.. it works.. hahaha..) By the way this is how i get UITextField inside searchBar..
First i tried what you have which is:
and i tried rearranging it like this...
again i tried your code under
-(void)viewDidLoad
but no good, yeah it is always nil as you said.. now it looks like this..i programmatically selectedTextRange(for example purposes) and probably it something to do with the xibs autolayout or something (weird i dont experience it) but there it is, try it.. :)
i hope i've helped you..
Happy coding.. Cheers!