Iphone: unable to load modal view from UITabBarController

I want to be able to present a modal view with a UIViewController instance. I have no problem with this when you present it from a standard UIViewController (root view). I configured a delegate to make the root view a private modal view. This is in line with Apple's best practice.

When I try to make the same root view as the modal view when the root view is loaded from the UITabBarController there are serious problems. The first three times I have no problem loading the view, but the fourth time the debugger shows that the root view is freed when I try to call the delegate method ("message sent to freed instance"). I am assuming that the root mode was auto-implemented while the view of the modality was shown. How can I avoid this?

The example I installed uses a template for UITabBarController representing the modal view from the first view:

FirstViewController.h (root view controller):

#import <UIKit/UIKit.h>

@protocol ModalDelegate;
@interface FirstViewController : UIViewController <ModalDelegate>{
}

-(IBAction)startPressed:(id)sender;
@end

      

FirstViewController.m:

#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

      

ModalViewController.h:

#import <UIKit/UIKit.h>

@protocol ModalDelegate;


@interface ModalViewController : UIViewController {
 id<ModalDelegate> delegate;

}

@property (assign) id<ModalDelegate> delegate;

- (IBAction)OKPressed:(id)sender;

@end

@protocol ModalDelegate <NSObject>

@optional
-(void)modal:(ModalViewController *)controller;

@end

      

ModalViewController.m:

#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

      

+2


source to share


2 answers


You shouldn't free the delegate in your ModalViewController implementation dealloc

, since you (correctly) didn't store it.

Alternatively, you can dismiss the modal view controller from yourself, rather than customizing the delegate. I find it easier. Here is a discussion of the iPhone Dev Center rejectModalViewControllerAnimated:



The parent view controller is responsible for abandoning the modal which it presented with the presentModalViewController: animated: method. If you call this method on the modal view controller itself, the modal view controller automatically forwards the message to its parent view controller.

+2


source


Once you use

@property (assign) id<ModalDelegate> delegate;

      

assign the type to the property, you shouldn't free the delegate in dealloc Your code will work fine if you use (save) the type of the property or don't free the delegate in dealloc.

So, you have two possible ways:



@property (retain) id<ModalDelegate> delegate;

      

or

- (void)dealloc {
 //[delegate release];
    [super dealloc];
}

      

0


source







All Articles