非常奇怪的NSInvalidArgumentException与addSubview FBLoginView [英] Very weird NSInvalidArgumentException with addSubview FBLoginView

查看:164
本文介绍了非常奇怪的NSInvalidArgumentException与addSubview FBLoginView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有2个视图的iOS应用程序,我试图将FBLoginView添加到两个视图中.视图1和视图2均已添加FBLoginView.该程序在视图1中启动.

I have an app for iOS with 2 views and I'm trying to add the FBLoginView to both of them. Both view 1 and view 2 has the FBLoginView added to them. The program starts in view 1.

方案1 :我在视图1中,尚未登录.我进入视图2,程序崩溃.

Scenario 1: I'm in view 1, not logged in. I enter view 2, the program crashes.

场景2 :我在视图1中,并且已登录.我进入视图2,该程序不会崩溃,并在视图2中显示FBLoginView,一切正常.

Scenario 2: I'm in view 1, and logged in. I enter view 2, the program does not crash and displays the FBLoginView in view 2, everything works fine.

场景3 :我在视图1中,未登录.登录后,我进入视图2,程序不会崩溃,并在视图2中显示FBLoginView,一切正常

Scenario 3: I'm in view 1, not logged in. I login, and then I enter view 2, the program does not crash and displays the FBLoginView in view 2, everything works fine.

在场景2和3中,我也可以在视图2中注销,这一切都很好.我什至可以注销后退出视图2,然后返回,它仍然可以. 程序崩溃的唯一情况是,如果我从头开始启动程序,以前没有登录,然后进入视图2 .然后我收到此错误消息:

In both scenario 2 and 3, i can also logout while in view 2 and it's all ok. I can even exit view 2 after logging out and come back and it's still ok. The only circumstances where the program crashes are if I start the program from scratch, I'm not previously logged in and I enter view 2. Then I get this error message:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' * -[__ NSCFConstantString stringByAppendingString:]:无参数'

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSCFConstantString stringByAppendingString:]: nil argument'

两个视图中的viewDidLoad-功能相同,它们是从HelloFacebookSample复制粘贴的:

The viewDidLoad-function are identical in both views, they are copy-pasted from the HelloFacebookSample:

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
FBLoginView *loginview = [[FBLoginView alloc] init];

loginview.frame = CGRectOffset(loginview.frame, 5, 5);
#ifdef __IPHONE_7_0
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        loginview.frame = CGRectOffset(loginview.frame, 5, 25);
    }
#endif
#endif
#endif
loginview.delegate = (id)self;

[self.view addSubview:loginview];

[loginview sizeToFit];

如果我在视图2中删除此行代码:[self.view addSubview:loginview];该程序不再崩溃.但是,通过写NSLog,我可以看到它并没有在那条线崩溃,而是通过了.实际上,通过在视图2中每个方法的开头和结尾处写入NSLog,我已经确认视图2中包含的所有代码均已通过.我的代码中没有导致崩溃的特定行.

If I remove this line of code in view 2: [self.view addSubview:loginview]; the program no longer crashes. However, by writing to NSLog I can see that it does not crash on that line, it passes. In fact, by writing to NSLog at the start and end of every method in view 2 I have confirmed that all the code I have inside view 2 passes. There is no specific line in my code that causes the crash.

根据我在Google搜索中收集到的信息,我唯一的猜测是FBLoginView不知道是否应该在视图2的FBLoginView中写用Facebook登录"或注销".在错误消息中.如上所述,我认为FBLoginView不能同时处理多个视图.

From what I have gathered with google searching, my only guess is that the FBLoginView doesn't know if it's supposed to write "Login with Facebook" or "Logout" in the FBLoginView in view 2, therefore the 'nil argument' in the error message. As said, I don't think the FBLoginView is meant to be able to handle being in several views at the same time.

有什么想法吗?我是否缺少某些东西,或者这是FBLoginView的问题?

Thoughts? Am I missing something or is this a problem with FBLoginView?

推荐答案

任何给定的视图一次只能是一个视图层次结构的一部分.如果您尝试将同一视图对象添加到多个视图中,那么您会遇到奇怪的崩溃,这也就不足为奇了. (通常,可以将同一视图类的不同实例添加到不同的视图中.)

Any given view should only be a part of one view hierarchy at a time. If you’re trying to add the same view object to multiple views then it’s not surprising that you’re getting weird crashes. (In general, it’s fine to add different instances of the same view class to different views.)

这篇关于非常奇怪的NSInvalidArgumentException与addSubview FBLoginView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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