3
votes

I have a programmatically created Storyboard (as I only use it for iOS 5) with a simple Controller and a iPad View. I need a gesture recognizer and added that. But when the App loads and I make the gustier I get EXC_BAD_ACCESS:

*** -[DOiPadStoryboardViewController handleSliding:]: message sent to deallocated instance 0x238580

It must be something simple I am doing wrong. Why is the controller deallocated?

I create the storyboard with the following code:

if (isIOS5) {
    // iOS 5 with storyboard
    UIViewController *viewControler = nil;
    if([self isiPad]){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPadStoryboard" bundle: [NSBundle mainBundle]];
        viewControler = [storyboard instantiateInitialViewController];
    } else {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard" bundle: [NSBundle mainBundle]];
        viewControler = [storyboard instantiateInitialViewController];
    }
    [self.window addSubview:viewControler.view];
} 

The gesture recognizer is connected to the main view and the IBAction is in the view controller. The storyboard looks as follows (but not sure if this add anything): Storyboard

and this is the Pan Gesture Recognizer: Gesture properties

and finally the 'bt' console output:

#0  0x37d2b8a0 in ___forwarding___ ()
#1  0x37c86680 in __forwarding_prep_0___ ()
#2  0x318d2f06 in _UIGestureRecognizerSendActions ()
#3  0x31864c1c in -[UIGestureRecognizer _updateGestureWithEvent:] ()
#4  0x31a90508 in ___UIGestureRecognizerUpdate_block_invoke_0541 ()
#5  0x317dfd68 in _UIGestureRecognizerApplyBlocksToArray ()
#6  0x317de8b6 in _UIGestureRecognizerUpdate ()
#7  0x317eb3cc in _UIGestureRecognizerUpdateGesturesFromSendEvent ()
#8  0x317eb20e in -[UIWindow _sendGesturesForEvent:] ()
#9  0x317eaddc in -[UIWindow sendEvent:] ()
#10 0x317d14ec in -[UIApplication sendEvent:] ()
#11 0x317d0d2c in _UIApplicationHandleEvent ()
#12 0x37a57df2 in PurpleEventCallback ()
#13 0x37cfd552 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#14 0x37cfd4f4 in __CFRunLoopDoSource1 ()
#15 0x37cfc342 in __CFRunLoopRun ()
#16 0x37c7f4dc in CFRunLoopRunSpecific ()
#17 0x37c7f3a4 in CFRunLoopRunInMode ()
#18 0x37a56fcc in GSEventRunModal ()
#19 0x317ff742 in UIApplicationMain ()
#20 0x00003050 in main (argc=1, argv=0x2fdffaa8)
2

2 Answers

2
votes

Set the view controller's to be your window's root view controller, instead of adding its view as a subview - which is obsolete and discouraged by Apple since you are detaching the views hierarchy from the view controllers hierarchy, which in turn will eventually cause nasty bugs such as your view controllers not getting rotation callbacks, etc. etc.

As a by the way, your view controller will be retained by the window, and no more crash.

1
votes

Just make viewControler a (retained) property. You are not retaining the view controller in your code example.

Create a property in the .h file:

@property (retain) UIViewController *viewController;

In the .m file add:

@synthesize viewController;

Also change the assignment:

self.viewControler = [storyboard instantiateInitialViewController];