iOS应用程序错误 - 无法将self添加为子视图 [英] iOS app error - Can't add self as subview

查看:303
本文介绍了iOS应用程序错误 - 无法将self添加为子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了此崩溃报告,但我不知道如何调试它。

I received this crash report, but I don't know how to debug it.

Fatal Exception NSInvalidArgumentException
Can't add self as subview
0 ...    CoreFoundation  __exceptionPreprocess + 130
1    libobjc.A.dylib     objc_exception_throw + 38
2    CoreFoundation  -[NSException initWithCoder:]
3    UIKit   -[UIView(Internal) _addSubview:positioned:relativeTo:] + 110
4    UIKit   -[UIView(Hierarchy) addSubview:] + 30
5    UIKit   __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke + 1196
6    UIKit   +[UIView(Animation) performWithoutAnimation:] + 72
7    UIKit   -[_UINavigationParallaxTransition animateTransition:] + 732
8    UIKit   -[UINavigationController _startCustomTransition:] + 2616
9    UIKit   -[UINavigationController _startDeferredTransitionIfNeeded:] + 418
10   UIKit   -[UINavigationController __viewWillLayoutSubviews] + 44
11   UIKit   -[UILayoutContainerView layoutSubviews] + 184
12   UIKit   -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 346
13   QuartzCore  -[CALayer layoutSublayers] + 142
14   QuartzCore  CA::Layer::layout_if_needed(CA::Transaction*) + 350
15   QuartzCore  CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
16   QuartzCore  CA::Context::commit_transaction(CA::Transaction*) + 228
17   QuartzCore  CA::Transaction::commit() + 314
18   QuartzCore  CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 56

iOS版本为7.0.3。
任何人遇到这种奇怪的崩溃?

The iOS version is 7.0.3. Anyone experience this weird crash?

更新:

我不知道我的位置在哪里代码导致此崩溃,所以我无法在此处发布代码,抱歉。

I don't know where in my code caused this crash, so I can not post the code here, sorry.

第二次更新

请参阅下面的答案。

推荐答案

我将在我的应用程序中描述有关此崩溃的更多详细信息,并将其标记为回答。

I will describe more details about this crash in my app and mark this as answered.

我的应用程序有一个UINavigationController,根控制器是一个UITableViewController,它包含一个note对象列表。 note对象在html中有一个content属性。选择一个注释将转到详细控制器。

My app has a UINavigationController with the root controller is a UITableViewController that contains a list of note objects. The note object has a content property in html. Select a note will go to the detail controller.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //get note object
    DetailViewController *controller = [[DetailViewController alloc] initWithNote:note];
    [self.navigationController pushViewController:controller animated:YES];
}



细节控制器



此控制器有一个UIWebView,显示从根控制器传递的注释内容。

Detail controller

This controller has a UIWebView, display the note content passed from the root controller.

- (void)viewDidLoad
{
    ...
    [_webView loadHTMLString:note.content baseURL:nil];
    ...
}

此控制器是webview控件的委托。如果笔记包含链接,请点按链接将转到应用内网页浏览器。

This controller is the delegate of the webview control. If the note contains links, tap a link will go to the in-app web browser.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    WebBrowserViewController *browserController = [[WebBrowserViewController alloc] init];
    browserController.startupURL = request.URL;
    [self.navigationController pushViewController:webViewController animated:YES];
    return NO;
}

我每天收到上述崩溃报告。我不知道我的代码在哪里造成了这次崩溃。经过一些用户的帮助调查,我终于能够解决这个崩溃。这个HTML内容会导致崩溃:

I received the above crash report everyday. I don't know where in my code caused this crash. After some investigates with the help of a user, I was finally able to fix this crash. This html content will cause the crash:

...
<iframe src="http://google.com"></iframe>
...

在详细控制器的viewDidLoad方法中,我将此html加载到webview控件,在此之后,上面的委托方法立即被调用request.URL是iframe的源(google.com)。这个委托方法在viewDidLoad => crash时调用pushViewController方法!

In the viewDidLoad method of the detail controller, I loaded this html to the webview control, right after that, the above delegate method was called immediately with request.URL is the iframe's source (google.com). This delegate method calls pushViewController method while in viewDidLoad => crash!

我通过检查navigationType修复了这个崩溃:

I fixed this crash by checking the navigationType:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType != UIWebViewNavigationTypeOther)
    {
        //go to web browser controller
    }
}

希望这有帮助

这篇关于iOS应用程序错误 - 无法将self添加为子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆