1

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?

NTNT
  • 541
  • 1
  • 7
  • 18
  • Are you sure that `searchField` is not `nil`? – rmaddy May 20 '15 at 03:53
  • Yes, i'm sure it is not nil – NTNT May 20 '15 at 03:56
  • 1
    Read the docs for `selectedTextRange`. It says it returns `nil` if there is no selection. Does the text field have the focus when you call these methods? – rmaddy May 20 '15 at 03:58
  • yes, i set focus to this textfield first by call [searchField becomeFirstResponder]; i already update my question. Do you know what's wrong in my code? Cannot understand why it is. – NTNT May 20 '15 at 04:11

1 Answers1

0

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..

for (id object in [[[self.searchBar subviews] objectAtIndex:0] subviews])
{
    if ([object isKindOfClass:[UITextField class]])
    {
        UITextField *searchField = (UITextField *)object;
        break;
    }
}

First i tried what you have which is:

UITextField *searchField = (UITextField *)[searchBar valueForKey:@"_searchField"];
searchBar.text = @"This is text";
// and logging your searchField.text here.. returns nil/empty.. 

and i tried rearranging it like this...

 searchBar.text = @"This is text";
 UITextField *searchField = (UITextField *)[searchBar valueForKey:@"_searchField"];
 NSLog(@"searchFields.text :%@", searchField.text);

again i tried your code under -(void)viewDidLoad but no good, yeah it is always nil as you said.. now it looks like this..

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    searchBar.text = @"This is text"; // not important

    UITextField *searchField = [searchBar valueForKey:@"_searchField"];
    [searchField becomeFirstResponder];

     UITextRange *selectedRange = [searchField selectedTextRange]; //selectedRange = not nil anymore

     NSLog(@"searchField.text :%@ -> selectedRange :%@\n\n" , searchField.text, selectedRange);
}
//i've also tried it inside uibutton's event and i worked .. >.< 

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!

0yeoj
  • 4,500
  • 3
  • 23
  • 41
  • Thanks but it still not working. i logging the searchfield and sure it's not nil. BTW, my searchbar is outlet from XIB, possible it's the cause? – NTNT May 20 '15 at 06:12
  • @NTNT.. Again i tried your code and yes i also think that it is cause by Xibs auto layout.. Hmm.. i placed your code inside: `- (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; }` i'll edit my answer.. – 0yeoj May 20 '15 at 07:28
  • Thanks. but how do u try on uibutton's event?I write it in 1 event but when call that event, it still show nil. I also tried putting my code in viewDidLayoutSubviews and it worked! so stress ... – NTNT May 20 '15 at 08:20