2

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.

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

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.

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.

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:

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

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

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.

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.

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

1 Answer 1

0

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.)

1
  • I guess it was kind of as I expected then, thank you!
    – Rkey
    May 5, 2014 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.