Note that there are some explanatory texts on larger screens.

plurals
  1. POIphone: Unable to load modal view from UITabBarController
    primarykey
    data
    text
    <p>I want to be able to present a modal view with an instance of a UIViewController. I have no problem doing this when presenting it from a standard UIViewController (the root view). I have set up a delegate to make the presenting root view close the modal view. This is according to Apple best practice.</p> <p>When I try to make the same root view present the modal view when the root view is loaded from a UITabBarController I get serious problems. The first three times I have no problem loading the view, but the fourth time the debugger shows that the the root view is deallocated when trying to call the delegate method ("message sent to deallocated instance"). My guess is that the root view has been autoreleased while the modal view was shown. How am I able to avoid this?</p> <p>The example I have set up uses the template for the the UITabBarController, presenting the modal view from the first view:</p> <p>FirstViewController.h (root view controller):</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @protocol ModalDelegate; @interface FirstViewController : UIViewController &lt;ModalDelegate&gt;{ } -(IBAction)startPressed:(id)sender; @end </code></pre> <p>FirstViewController.m:</p> <pre><code>#import "FirstViewController.h" #import "ModalViewController.h" @implementation FirstViewController -(IBAction)startPressed:(id)sender { ModalViewController *modal=[[ModalViewController alloc] init]; modal.delegate=self; [self presentModalViewController:modal animated:TRUE]; [modal release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)dealloc { [super dealloc]; } #pragma mark Modal Delegate -(void)modal:(ModalViewController *)controller { [self dismissModalViewControllerAnimated:YES]; } @end </code></pre> <p>ModalViewController.h:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @protocol ModalDelegate; @interface ModalViewController : UIViewController { id&lt;ModalDelegate&gt; delegate; } @property (assign) id&lt;ModalDelegate&gt; delegate; - (IBAction)OKPressed:(id)sender; @end @protocol ModalDelegate &lt;NSObject&gt; @optional -(void)modal:(ModalViewController *)controller; @end </code></pre> <p>ModalViewController.m:</p> <pre><code>#import "ModalViewController.h" @implementation ModalViewController @synthesize delegate; - (IBAction)OKPressed:(id)sender { if ([self.delegate respondsToSelector:@selector(modal:)]) //Check to see if method responds to selector { [self.delegate modal:self]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)dealloc { [delegate release]; [super dealloc]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload