Objective c 在UISearchBar中键入会使我的应用程序崩溃

Objective c 在UISearchBar中键入会使我的应用程序崩溃,objective-c,uitableview,ios8,uisearchbar,uisearchcontroller,Objective C,Uitableview,Ios8,Uisearchbar,Uisearchcontroller,更新2 在用户点击ViewController1中的按钮后,我的应用程序在以模式显示时崩溃。在我的故事板中,我有一个标准的present modallysegue设置,用于弹出包含ui搜索栏的UINavController/UITableViewController。每次都失败了 但是,在我的AppDelegate中,如果我将窗口的rootViewController设置为相同的UINavController/UITableViewController,则一切都会正常工作 由于某种原因,通过se

更新2

在用户点击ViewController1中的按钮后,我的应用程序在以模式显示时崩溃。在我的故事板中,我有一个标准的
present modally
segue设置,用于弹出包含
ui搜索栏的UINavController/UITableViewController
。每次都失败了

但是,在我的
AppDelegate
中,如果我将窗口的
rootViewController
设置为相同的UINavController/UITableViewController,则一切都会正常工作

由于某种原因,通过segue进行转换,然后执行UISearchBar会导致崩溃

更新1

我现在在点击一个字母时收到以下错误:

2015-02-03 12:23:35.262 Afar D[28348:2740681] -[NSNull length]: unrecognized selector sent to instance 0x10e352ce0
2015-02-03 12:23:40.313 Afar D[28348:2740681] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x10e352ce0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e0a9f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010dd42bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010e0b104d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010e00927c ___forwarding___ + 988
    4   CoreFoundation                      0x000000010e008e18 _CF_forwarding_prep_0 + 120
    5   CoreFoundation                      0x000000010df89a7b CFStringCompareWithOptionsAndLocale + 219
    6   Foundation                          0x000000010d6822b7 -[NSString compare:options:range:] + 29
    7   UIKit                               0x000000010c5f53b4 -[UIPhysicalKeyboardEvent _matchesKeyCommand:] + 224
    8   UIKit                               0x000000010c53c12e -[UIResponder(Internal) _keyCommandForEvent:] + 285
    9   UIKit                               0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
    10  UIKit                               0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
    11  UIKit                               0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
    12  UIKit                               0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
    13  UIKit                               0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
    14  UIKit                               0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
    15  UIKit                               0x000000010c53c197 -[UIResponder(Internal) _keyCommandForEvent:] + 390
    16  UIKit                               0x000000010c3d8f0a -[UIApplication _handleKeyUIEvent:] + 126
    17  UIKit                               0x000000010c5c7fcc -[UIKeyboardImpl _handleKeyEvent:executionContext:] + 66
    18  UIKit                               0x000000010c75bbb7 -[UIKeyboardLayoutStar completeRetestForTouchUp:timestamp:interval:executionContext:] + 3611
    19  UIKit                               0x000000010c75a8e5 -[UIKeyboardLayoutStar touchUp:executionContext:] + 1374
    20  UIKit                               0x000000010c5d531b __28-[UIKeyboardLayout touchUp:]_block_invoke + 242
    21  UIKit                               0x000000010cb23914 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 332
    22  UIKit                               0x000000010c5d521c -[UIKeyboardLayout touchUp:] + 252
    23  UIKit                               0x000000010c5d5cc6 -[UIKeyboardLayout touchesEnded:withEvent:] + 319
    24  UIKit                               0x000000010c40b308 -[UIWindow _sendTouchesForEvent:] + 735
    25  UIKit                               0x000000010c40bc33 -[UIWindow sendEvent:] + 683
    26  UIKit                               0x000000010c3d89b1 -[UIApplication sendEvent:] + 246
    27  UIKit                               0x000000010c3e5a7d _UIApplicationHandleEventFromQueueEvent + 17370
    28  UIKit                               0x000000010c3c1103 _UIApplicationHandleEventQueue + 1961
    29  CoreFoundation                      0x000000010dfdf551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    30  CoreFoundation                      0x000000010dfd541d __CFRunLoopDoSources0 + 269
    31  CoreFoundation                      0x000000010dfd4a54 __CFRunLoopRun + 868
    32  CoreFoundation                      0x000000010dfd4486 CFRunLoopRunSpecific + 470
    33  GraphicsServices                    0x000000010fc109f0 GSEventRunModal + 161
    34  UIKit                               0x000000010c3c4420 UIApplicationMain + 1282
    35  My App                              0x000000010a5e1c63 main + 115
    36  libdyld.dylib                       0x000000010e75a145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我正在使用iOS8新的
UISearchController
来管理
UISearchBar
以过滤表中的数据。如果我激活
UISearchBar
,只要我输入第一个字母,应用程序就会崩溃(有趣的是,屏幕上的键盘不会出现在模拟器中,但我认为这与此无关)

为了缩小范围,我对
UIViewController
中的几乎所有内容都进行了注释,以便
UITableView
不呈现任何内容。我已经实现了
UISearchControllerDelegate
UISearchBarDelegate
,这样我就可以在调用每个方法时记录日志。但是,在键入第一个字母后,我没有收到任何控制台消息。我甚至不知道在哪里可以设置断点

下面是我得到的碰撞:

-[NSNull length]: unrecognized selector sent to instance 0x107602ce0
请注意,在我的文件中,我没有调用任何长度。幕后有些事,但我不知道是什么

以下是我设置UISearchController的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.definesPresentationContext = true;
    self.searchController.hidesNavigationBarDuringPresentation = false;
    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchController.searchBar;

    self.searchController.searchBar.delegate = self;
    self.searchController.delegate = self;
}
根据请求,以下是我的委托方法(实现的目的只是为了查看在键入第一个字母后是否调用了任何内容,而不是):


NSNull只是一个用于表示null的单例对象。传统上,它用于在不能处理nil条目的容器(数组和字典)中包装nil值

解析json并尝试将结果添加到集合中时,经常会出现此错误

您是否在委托方法中执行类似操作


编辑:根据您发送给该NSNull(长度)的消息判断,对于您请求的字符串,您可能会得到一个“空”结果,并且预期结果不会为零。

在花了整整一天的时间来解决这个问题后,它归结为一个完全无关的问题:情节提要损坏


建议查找损坏的场景并重新创建它。不幸的是,我无法确定这一点,因此经过一些尝试和错误,它最终成为了我故事板中的初始视图控制器,而这恰好是一个
UINavigationController
。我删除了它,重新添加了它,一切都是金色的。

您是否能够为搜索栏提供代理代码?您正在向空对象发送一条“长度”消息。我猜搜索栏中的文本是空的,控制器中的某些内容正在尝试读取其长度。如果没有更多关于在何处设置搜索内容或委托方法的代码,很难说清楚。请参见self
self.searchController.definesPresentationContext=YES
请注意,您应该使用
YES
NO
而不是
true
false
@flexaddicted,我已经实现了UISearchBar委托方法,并且只记录了方法的名称。但是,在UISearchBar中键入第一个字母后,不会调用UISearchBarDelegate方法。我将
definesPresentationContext
切换为true,但这没有帮助。您可以添加委托代码吗?感谢您的提示,但为了降低复杂性,我基本上注释掉了代码中的所有内容,但viewDidLoad(上面)和所需的TableView委托/数据源方法(它呈现一个0行的空表)除外,以及所有UISearchBarDelegate、UISearchController委托和UISearchResultsUpdated委托方法。所有这些方法都只是记录它们的方法名。在我的文件中,没有对任何内容调用length,并且在键入第一个字母后,没有调用委托方法(很明显,我的控制台中没有任何内容),那么下一步将是在表视图中搜索nsstring。特别是UILabel实例。如果你把它们拿走呢?您是否持有对其中任何一个的弱引用?我已经从我的表视图datasource/delegate方法中注释掉了所有内容,所以屏幕上没有显示任何单元格。我的文件中也没有字符串。奇怪的
#pragma mark - UISearchResultsUpdating

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

#pragma mark - UISearchControllerDelegate

- (void)didDismissSearchController:(UISearchController *)searchController
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)didPresentSearchController:(UISearchController *)searchController
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)presentSearchController:(UISearchController *)searchController
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)willDismissSearchController:(UISearchController *)searchController
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)willPresentSearchController:(UISearchController *)searchController
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

#pragma mark - UISearchBarDelegate

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return true;
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return true;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return true;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}