0

I have an app that uses a transition file to flip from page to page. I am using ARC and works just fine on 5.1, but crashes all the time on 4.3 simulator. Looking at the thread and the extended detail from instruments, it points to 2 lines of code (shown below) with the error: EXC_BREAKPOINT (code=EXC_I386_BPT). Looks like the UIViewController is being deallocated. Not sure how to fix this. Thanks in advance for any help you can offer!

@synthesize containerView = _containerView;
@synthesize viewController = _viewController;  //ERROR LINE#1

- (id)initWithViewController:(UIViewController *)viewController
{
    if (self = [super init]) 
    {
      _viewController = viewController;
    }
    return self;
}

- (void)loadView
{
    self.wantsFullScreenLayout = YES;
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    self.view = view;

    _containerView = [[UIView alloc] initWithFrame:view.bounds];
    _containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_containerView];

    [_containerView addSubview:self.viewController.view];
}

- (void)transitionToViewController:(UIViewController *)aViewController
                   withOptions:(UIViewAnimationOptions)options
{    
    aViewController.view.frame = self.containerView.bounds;

    [UIView transitionWithView:self.containerView
                      duration:0.65f
                       options:options
                    animations:^{ [self.viewController.view removeFromSuperview];
                        [self.containerView addSubview:aViewController.view];}
                    completion:^(BOOL finished)
   //ERROR LINE#2                 { self.viewController = aViewController;}];
}
ctw
  • 97
  • 1
  • 11
  • where do you call transitionToViewController? – Omar Abdelhafith Jun 16 '12 at 13:21
  • I have a ViewController for each page. Here is the line of code I use to call it. TitlePageViewController *tpvc=[[TitlePageViewController alloc] init]; AppDelegate *appDelegate = [UIApplication sharedApplication].delegate; [appDelegate.transitionController transitionToViewController:tpvc withOptions:UIViewAnimationOptionTransitionCurlUp]; – ctw Jun 16 '12 at 13:30

1 Answers1

0

You should not be transitioning views in and out from underneath their respective view controllers. You might get it to work, but it's fragile, at best.

If you want to future-proof your code, you should be transitioning between view controllers and let them handle their own views, but wrap that in the desired animation if you don't like the default animation. So, you should either:

  1. Just do simple presentViewController or pushViewController to go to the next controller and dismissViewController or popViewController to return; or

  2. In iOS 5, you can do your own container view controller (see session 102 in WWDC 2011 or see the discussion of view controller containment in the UIViewController reference) and then you can transitionFromViewController.

If we knew more about your app flow, why you're doing what you're doing, we can probably advise you further.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks for your help. The app is a short storybook. I had decided to use the transitionController because I thought it provided better animation options. But since it seems that it is a fragile design, I can go back to the simple pushViewController method. Thanks for the links to WWDC, I will take a good look at that to see if there are some other options that may be helpful. – ctw Jun 16 '12 at 14:12
  • @ctw If your intuition was that you didn't want to push view controllers as you went from page to page, you're probably right. If I wanted to go old school, I'd probably create own UIView subclass for the displayed page and animate the transition from page to page with that, rather than pushing view controllers. By the way, it might be worth checking out the [Page View Controller](http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/PageViewControllers.html), too. – Rob Jun 16 '12 at 14:23
  • @Ryan - Yes the PageViewController is exactly what I wanted to do! But I couldn't figure out how to setup each page with its own view controller. It seemed like each page was just a static page...not one where I could set up a new view controller with its own xib for each page. I got so frustrated I gave up. I think I will go back and give it another try. – ctw Jun 16 '12 at 14:55
  • @ctw If you google "PageViewController example" I think you'll see a lot of hits that will point you in the right direction. Good luck! – Rob Jun 16 '12 at 15:02