0

I have an exception happening that I just can't figure out. I have this bit of code here:

newControllers = [[NSMutableArray alloc] initWithCapacity:9];  // Only allocate what we need


    // Ok, add the new thumb UIs
    for (int i = 0; i < 3; ++i) {

        for (int j = 0; j < 3; ++j) {

            // Create the view controller
            ThumbViewController *newThumbVC = [[ThumbViewController alloc]
                                                   initWithNibName:@"NewThumbDisplayView" bundle:nil];
            // Set the info
            newThumbVC.localInfo = [newInfo objectAtIndex:(i * 3) + j];

            // Place it properly
            [self.scrollViewContent addSubview:newThumbVC.view];
            CGRect rect = CGRectMake(8 + (j * 99), 363 + (i * 134), 106, 142);
            newThumbVC.view.frame = rect;
            [self.scrollViewContent bringSubviewToFront:newThumbVC.view];

            [newControllers addObject:newThumbVC];
        }
    }

When running on the simulator, this works just perfectly. This morning I tried to run it on my phone, and I get an exception when calling CGRectMake with the following stack (Note that nothing is printed out in the output window at all making this more of a pain to figure out).

Thread 1, Queue : com.apple.main-thread

    #0  0x35220238 in objc_exception_throw ()
    #1  0x3751b788 in +[NSException raise:format:arguments:] ()

If anybody can point out to me what exactly is not right here, I would be very grateful.

Cezar
  • 55,636
  • 19
  • 86
  • 87
Jason
  • 5,277
  • 4
  • 17
  • 12
  • Can you replace the calculated values being passed to CGRectMake with constants and see if you still get the exception? Try floats instead of ints just to be sure. – Chris Trahey Jun 15 '12 at 15:36

3 Answers3

1

CGRectMake is just a macro so it's not the problem. You really only need one view controller and have it manage a set of views rather than have a set of controllers. Having multiple controllers is highly discouraged.

ahwulf
  • 2,584
  • 15
  • 29
  • See above :) It was a mismatch between my iOS version on the device and my xcode version. – Jason Jun 15 '12 at 21:16
  • Also, thanks for the tip about multiple view controllers being a bad thing. I've since changed my implementation and my thumbs are all now UIViews. – Jason Jun 16 '12 at 15:47
0

I think you're seeing a race condition on building the ThumbViewController's views which are built lazily after the nib is loaded. I think the crash is happening when you add the new vc's view as a subview, which on the simulator might get built quickly enough to be non-nil.

The SDK does not encourage multiple VCs in charge at once (with only a few exceptions like MPMoviePlayerController). Do you really need VCs for the thumbs? Just by the name, they sound more like views.

If you must use VCs, then you'll need to pass them their row/col and have them frame themselves in viewDidLoad (or later).

danh
  • 62,181
  • 10
  • 95
  • 136
  • Actually, it turned out to be a mismatch between my iOS version on my phone and my version of Xcode. So, these are not "real" view controllers in the same sense as a page one. These are a sort of mini viewcontrollers if you will that needs to manage data exchanges and presentation on it's view. – Jason Jun 15 '12 at 21:15
  • So after reading this post I looked into the general use case of having multiple VCs imbedded. So yeah, everybody seems to suggest not doing it, so I've changed the thumb class to be a derived from UIView instead. Thanks for the tip. – Jason Jun 16 '12 at 15:46
0

This turned out to be a problem with a version mismatch between my iOS version on my device and the version of XCode I was running. Updating XCode took care of everything.

Jason
  • 5,277
  • 4
  • 17
  • 12