0
votes

I have a UISearchBar programatically and works but if I try to set a placeholder and user tap to search something my app crash. This is my code:

Property

@property (strong, nonatomic) UISearchBar *mySearchBar;

Implementation

self.mySearchBar = [[UISearchBar alloc] init];
self.mySearchBar.delegate               = self;
self.mySearchBar.autoresizingMask       = UIViewAutoresizingFlexibleWidth;
self.mySearchBar.autocorrectionType     = UITextAutocorrectionTypeNo;
self.mySearchBar.translucent            = NO;
self.mySearchBar.returnKeyType          = UIReturnKeySearch;
self.mySearchBar.autocorrectionType    = UITextAutocorrectionTypeNo;
self.mySearchBar.inputAccessoryView     = toolBarKeyboard;
self.mySearchBar.placeholder            = @"Artist, Team or City";

This line makes crash when user tap in uisearchbar to find some data

 self.mySearchBar.placeholder            = @"Artist, Team or City";

Someone else with this error?

I use:

  • Xcode 6
  • iOS 8
  • iPhone 5 / 5s
1
This is the error: -[SearchViewController _searchController]: unrecognized selector sent to instance 0x7a09f0f0Mxnuel Salinas
Is SearchViewController your class? Does your class try to access the _searchController property/ivar? Show that code.rmaddy
Yes, SearchViewController is my class and i create a UISearchBar like string property and add it in navigation bar: self.navigationItem.titleView = self.mySearchBar;Mxnuel Salinas
That's not what I asked. Do you have any code that makes use of the _searchController value?rmaddy
Hi, Pardon me. Yes i found the error. I Have this code uncommented to take control on uisearchbar's uitextfield: UIView *viewToGetClearButton = [self.mySearchBar.subviews objectAtIndex:0]; for (UIView *view in viewToGetClearButton.subviews) { if ([view isKindOfClass: [UITextField class]]) { UITextField *txt = (UITextField *)view; txt.delegate = self; break; } } I Just delete this code and works!Mxnuel Salinas

1 Answers

2
votes

I know this is an old question, but if someone else is facing this issue, this can solve the problem:

UITextField* tfSearchBar = [self.mySearchBar valueForKey:@"_searchField"];
tfSearchBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Artist, Team or City"];

Hope it helps