0

我花了几个小时寻找这个问题的解决方案,但我什至找不到任何经历过它的人,更不用说解决方案了。

在我的项目中,我有一个主视图控制器,它推送包含 UIWebView 的第二个视图控制器。两个视图控制器都嵌入在导航控制器中。push segue 和 WebView 都使用 IB 连接起来。

转场按预期发生,网页加载并正常工作,直到我尝试输入。点击文本字段会调出键盘,但第一次按键会使应用程序崩溃并出现错误:

-[NSNull length]: unrecognized selector sent to instance 0x1899068
2014-02-06 04:37:19.550 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x1899068'
*** First throw call stack:
(
    0   CoreFoundation                      0x0174d5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014d08b6 objc_exception_throw + 44
...

这发生在模拟器和设备上。我特别困惑,因为我没有做任何复杂的事情。在包含 WebView 的视图控制器中,我有以下 viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [self.webView loadRequest:request];
}

并且没有其他代码。Google 在这里被用作示例站点,但它发生在我尝试过的每个站点上。奇怪的是,如果我删除 segue 并在没有父导航控制器的情况下呈现 Web 视图,一切正常,所以看起来导航控制器与它有关。正如我所说,webView 属性被连接为一个 IB 插座。我也尝试过以编程方式添加它,但结果是一样的。据我所知,我的第一个视图控制器中的任何内容都不应该对此产生任何影响;就像我说的那样,这是一个按钮的故事板segue。我可以确定向 NSNull 发送长度不是我自己直接做的任何事情,因为我没有在代码中的任何地方使用长度选择器。

任何帮助将不胜感激。

已请求它,因此完整的错误消息如下:

-[NSNull length]: unrecognized selector sent to instance 0x1899068
2014-02-06 05:18:29.607 AppName[859:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x1899068'
*** First throw call stack:
(
    0   CoreFoundation                      0x0174d5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014d08b6 objc_exception_throw + 44
    2   CoreFoundation                      0x017ea903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0173d90b ___forwarding___ + 1019
    4   CoreFoundation                      0x0173d4ee _CF_forwarding_prep_0 + 14
    5   CoreFoundation                      0x016cd95c CFStringGetLength + 140
    6   CoreFoundation                      0x016e1284 CFStringCompareWithOptionsAndLocale + 52
    7   Foundation                          0x010db634 -[NSString compare:options:range:locale:] + 175
    8   Foundation                          0x010db580 -[NSString compare:options:range:] + 69
    9   Foundation                          0x010eda59 -[NSString caseInsensitiveCompare:] + 80
    10  UIKit                               0x00454f48 -[UIPhysicalKeyboardEvent _matchesKeyCommand:] + 280
    11  UIKit                               0x00398ac9 -[UIResponder(Internal) _keyCommandForEvent:] + 312
    12  UIKit                               0x00398b3b -[UIResponder(Internal) _keyCommandForEvent:] + 426
    13  UIKit                               0x00398b3b -[UIResponder(Internal) _keyCommandForEvent:] + 426
    14  UIKit                               0x00398b3b -[UIResponder(Internal) _keyCommandForEvent:] + 426
    15  UIKit                               0x00398b3b -[UIResponder(Internal) _keyCommandForEvent:] + 426
    16  UIKit                               0x00398b3b -[UIResponder(Internal) _keyCommandForEvent:] + 426
    17  UIKit                               0x00398b3b -[UIResponder(Internal) _keyCommandForEvent:] + 426
    18  UIKit                               0x00398b3b -[UIResponder(Internal) _keyCommandForEvent:] + 426
    19  UIKit                               0x00398b3b -[UIResponder(Internal) _keyCommandForEvent:] + 426
    20  UIKit                               0x00252176 -[UIApplication handleKeyHIDEvent:] + 226
    21  UIKit                               0x0023a07c _UIApplicationHandleEventQueue + 2954
    22  CoreFoundation                      0x016d683f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    23  CoreFoundation                      0x016d61cb __CFRunLoopDoSources0 + 235
    24  CoreFoundation                      0x016f329e __CFRunLoopRun + 910
    25  CoreFoundation                      0x016f2ac3 CFRunLoopRunSpecific + 467
    26  CoreFoundation                      0x016f28db CFRunLoopRunInMode + 123
    27  GraphicsServices                    0x036f29e2 GSEventRunModal + 192
    28  GraphicsServices                    0x036f2809 GSEventRun + 104
    29  UIKit                               0x0023ed3b UIApplicationMain + 1225
    30  AppName                             0x00003d3d main + 141
    31  libdyld.dylib                       0x01d8b70d start + 1
    32  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
4

2 回答 2

1

所以我设法通过不做任何事情来解决这个问题。最终修复它的是删除所有 VC 嵌入的导航控制器,然后将它们重新嵌入到导航控制器中。现在它完全可以工作了。所以我不知道这是一个错误的设置还是什么。

于 2014-02-07T21:35:00.073 回答
0

@deergomoo 对不起我的第一个答案。我洗它是假的。

那么,您如何看待下一个示例?它与您的项目结构相似吗?我找不到崩溃。

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UINavigationController *navController=[UINavigationController new];
    ViewController *viewController=[[ViewController alloc] init];
    [navController pushViewController:viewController animated:NO];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];

    return YES;
}

.

#import "ViewController.h"

@implementation ViewController

-  (void)viewDidLoad {
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                          style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                                                   reuseIdentifier:nil];
    cell.textLabel.text = @"Cell";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"didSelectRowAtIndexPath");
    UIViewController *viewController = [UIViewController new];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [viewController.view addSubview:webView];

    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [webView loadRequest:request];

    [self.navigationController pushViewController:viewController animated:TRUE];
}

@end
于 2014-02-06T09:52:32.017 回答