selectedTextRange return UISearchbar 中为零 iOS
selectedTextRange return nil in UISearchbar iOS
我有一个UISearchBar
,我得到了UITextfield
,然后我想设置选定的文本范围,但它总是return nil。这是我的代码:
UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];
[searchField becomeFirstResponder]; // focus to this searchfield
还有我填写的文字:
self.searchBar.text = @"This is text";
并检查 UITextField
文本是否已填写并且确实如此。尽管如此,关于UITextPosition
和UITextRange
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
我的代码有错吗?
您尝试过记录 UITextField *searchField
吗?从它的外观来看它是零..我认为你的代码应该是..
(UITextField *)[self.searchBar valueForKey:@"_searchField"]
([在尝试你的代码之后] OooOoKayyy.. 它有效.. 哈哈哈..)顺便说一下,这就是我在 searchBar 中获取 UITextField 的方式..
for (id object in [[[self.searchBar subviews] objectAtIndex:0] subviews])
{
if ([object isKindOfClass:[UITextField class]])
{
UITextField *searchField = (UITextField *)object;
break;
}
}
首先我尝试了你所拥有的是:
UITextField *searchField = (UITextField *)[searchBar valueForKey:@"_searchField"];
searchBar.text = @"This is text";
// and logging your searchField.text here.. returns nil/empty..
我试过这样重新排列它...
searchBar.text = @"This is text";
UITextField *searchField = (UITextField *)[searchBar valueForKey:@"_searchField"];
NSLog(@"searchFields.text :%@", searchField.text);
我再次在 -(void)viewDidLoad
下尝试了您的代码,但没有用,是的,正如您所说,它始终为零..现在看起来像这样..
- (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 .. >.<
我以编程方式选择了 TextRange(出于示例目的),可能与 xibs 自动布局或其他东西有关(很奇怪,我没有体验过)但它就在那里,试试吧.. :)
希望对你有所帮助..
编码愉快..干杯!
我有一个UISearchBar
,我得到了UITextfield
,然后我想设置选定的文本范围,但它总是return nil。这是我的代码:
UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];
[searchField becomeFirstResponder]; // focus to this searchfield
还有我填写的文字:
self.searchBar.text = @"This is text";
并检查 UITextField
文本是否已填写并且确实如此。尽管如此,关于UITextPosition
和UITextRange
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
我的代码有错吗?
您尝试过记录 UITextField *searchField
吗?从它的外观来看它是零..我认为你的代码应该是..
(UITextField *)[self.searchBar valueForKey:@"_searchField"]
([在尝试你的代码之后] OooOoKayyy.. 它有效.. 哈哈哈..)顺便说一下,这就是我在 searchBar 中获取 UITextField 的方式..
for (id object in [[[self.searchBar subviews] objectAtIndex:0] subviews])
{
if ([object isKindOfClass:[UITextField class]])
{
UITextField *searchField = (UITextField *)object;
break;
}
}
首先我尝试了你所拥有的是:
UITextField *searchField = (UITextField *)[searchBar valueForKey:@"_searchField"];
searchBar.text = @"This is text";
// and logging your searchField.text here.. returns nil/empty..
我试过这样重新排列它...
searchBar.text = @"This is text";
UITextField *searchField = (UITextField *)[searchBar valueForKey:@"_searchField"];
NSLog(@"searchFields.text :%@", searchField.text);
我再次在 -(void)viewDidLoad
下尝试了您的代码,但没有用,是的,正如您所说,它始终为零..现在看起来像这样..
- (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 .. >.<
我以编程方式选择了 TextRange(出于示例目的),可能与 xibs 自动布局或其他东西有关(很奇怪,我没有体验过)但它就在那里,试试吧.. :)
希望对你有所帮助..
编码愉快..干杯!