【问题标题】:UISearchBar placeholder crash in iOS 8iOS 8 中的 UISearchBar 占位符崩溃
【发布时间】:2015-04-06 22:03:10
【问题描述】:

我以编程方式有一个 UISearchBar 并且可以工作,但是如果我尝试设置占位符并用户点击以搜索我的应用程序崩溃的内容。这是我的代码:

属性

@property (strong, nonatomic) UISearchBar *mySearchBar;

实施

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";

当用户在 uisearchbar 中点击以查找某些数据时,此行会导致崩溃

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

其他人有此错误?

我用:

  • Xcode 6
  • iOS 8
  • iPhone 5 / 5s

【问题讨论】:

  • 这是错误:-[SearchViewController _searchController]: unrecognized selector sent to instance 0x7a09f0f0
  • SearchViewController 是你的班级吗?您的班级是否尝试访问 _searchController 属性/ivar?显示该代码。
  • 是的,SearchViewController 是我的课程,我创建了一个 UISearchBar 类似字符串属性并将其添加到导航栏中:self.navigationItem.titleView = self.mySearchBar;
  • 这不是我问的。你有任何使用_searchController 值的代码吗?
  • 嗨,对不起。是的,我发现了错误。我没有注释此代码以控制 uisearchbar 的 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 = 自我;休息; } } 我只是删除了这段代码就可以了!

标签: objective-c ios8 uisearchbar xcode6 placeholder


【解决方案1】:

我知道这是一个老问题,但如果其他人面临这个问题,这可以解决问题:

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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2018-06-07
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多