6

I have a regular UIButton on my main menu that currently launches a UIViewController; the contents of the corresponding .m file is as follows:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    documentPath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"pdf"];
    NSURL *targetURL = [NSURL fileURLWithPath:documentPath];

    document = [UIDocumentInteractionController interactionControllerWithURL: targetURL];
    document.delegate = self;
    [document retain];

    return self;
}

-(UIViewController *)documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
{
    return self;
}

-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [document autorelease];
}

-(void)viewDidLoad
{
    [super viewDidLoad];

    [document presentPreviewAnimated: YES]; // ** CRASH **
}

-(void)viewDidUnload
{
    [super viewDidUnload];
}

-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

-(void)dealloc
{
    [super dealloc];
}

My pdf file loads as expected, however when I hit the "done" button the document closes and I am left staring at my blank UIViewController - arguably as expected. But if I hit the navigation "back" button then the app crashes with a bad access error inside my viewDidLoad method, where the call to presentPreviewAnimated is found.

If somebody could please take a look, I would be most grateful.

(btw, there is no NIB file when this view controller is created. Yes, this in itself is wrong)

Luke
  • 11,426
  • 43
  • 60
  • 69
  • I think your problem may be with the autorelease you're doing in the delegate (together with the retain in the initWithNib method). – onnoweb May 27 '11 at 14:32
  • @onnoweb Without the retain it will crash with the following: Line 1: -[__NSCFType presentPreviewAnimated:]: unrecognized selector sent to instance 0x1a32e0 Line 2: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType presentPreviewAnimated:]: unrecognized selector sent to instance 0x1a32e0' The reason I added the retain was because Apple tell us to in the UIDocumentInteractionControllerDelegate protocol reference. – Luke May 27 '11 at 14:37
  • Hmm... I use UIDocument quite a bit in one of my apps, don't do a retain and have no issues. What if you move the UIDocument code from initWithNib to viewDidLoad? – onnoweb May 27 '11 at 14:56
  • Removing the autorelease and retain I had in, and moving the remaining 4 lines to viewDidLoad produces the same bad access error when I hit the "done" button. If I may ask, is your method to open a document the same/similar to what I have done so far? – Luke May 27 '11 at 15:02
  • Yes, although I do it in response to a user action in a view and not during view creation. – onnoweb May 27 '11 at 15:04
  • Should I instead create a UIView inside this UIViewController and try to get the document inside the view? Then I assume you just add the document controller as a subview of the new UIView? – Luke May 27 '11 at 15:07
  • 1
    I am wondering whether the issue is that you do this during view creation. So that when the user closes the document preview, it gets back to an incompletely formed UIView. So perhaps first build and load the view and then do UIDocument from viewDidAppear ? – onnoweb May 27 '11 at 15:12
  • 1
    Perfect my friend! Putting the init code for the UIDocumentInteractionController inside viewDidAppear: alongside a BOOL that tracked whether it was the first time viewed or not has worked perfectly. If it's the first view, the document is created and pushed. Once I hit "done," viewDidAppear fires again except that now my BOOL reads as NO, (not the first view) so I call popToRootViewControllerAnimated: which works well. Thank you for your support with this query. – Luke May 27 '11 at 15:32
  • If you add your second to last comment as an answer, I can flag this as solved :) – Luke May 27 '11 at 15:48

1 Answers1

1

I am wondering whether the issue is that you do this during view creation. So that when the user closes the document preview, it gets back to an incompletely formed UIView. So perhaps first build and load the view and then do UIDocument from viewDidAppear ?

onnoweb
  • 3,038
  • 22
  • 29