views:

48

answers:

1

I have an app that switches around to about 10 different view controllers with methods like this:

 -(IBAction)pg2button{
      pg2 *pg2view = [[pg2 alloc] initWithNibName: nil bundle: nil];
      pg2view.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
      [self presentModalViewController:pg2view animated:YES];
      [pg2view release];
  }

Where is a good place to release the current view before the next one is presented? Thanks!

+2  A: 

(1) Format your question: add at least 4 spaces before each code line.

(2) Why do you use initWithNibName:bundle: initializer if you pass nil as NIB name?
Just use the regular init.

(3) I see that you already release the view controller.
It will be released one more time (in the background) once you will dismiss it.

(4) If you've meant to ask "Where is a good place to dismiss the current view before the next one is presented?" then it depends on your structure.
Usually, the best approach is to add a delegate method in the original view controller, the modal view controller will call that delegate method and the original view controller will dismiss the modal one like this: [self dismissModalViewControllerAnimated:YES];.

EDIT:
Code sample for the delegate:

// The protocol that your original view controller should implement
@protocol ModalViewControllerDelegate <NSObject>

@required
- (void)modalViewControllerDidCancel:(UIViewController *)modalViewController;
- (void)modalViewController:(UIViewController *)modalViewController didReturnWithResult:(NSObject)result;

@end

This is how to implement:

@interface MainViewController : UIViewController <ModalViewControllerDelegate> {

    ...

}

...

@end

@implementation MainViewController

...

#pragma mark -
#pragma mark ModalViewControllerDelegate methods

- (void)modalViewControllerDidCancel:(UIViewController *)modalViewController {
    [self dismissModalViewControllerAnimated:YES];
}

- (void)modalViewController:(UIViewController *)modalViewController didReturnWithResult:(NSObject)result {
    // TODO: Do something with the result

    [self dismissModalViewControllerAnimated:YES];
}

...

@end

You should add the next code to your modal view controllers:

@interface ModalViewController1 : UIViewController {

    ...

    id<ModalViewControllerDelegate> delegate;

    ...

}

@property (assign) id<ModalViewControllerDelegate> delegate;

...

@end


@implementation ModalViewController1

@synthesize delegate;

...

- (void)cancelUserAction {

    ...

    [self.delegate modalViewControllerDidCancel:self];
}

@end



Don't forget also to set the delegate property to self (from MainViewController) once you create the modal view controller...

Michael Kessler
Thanks. It seems I do need to go the delegate route but i'm having a hard time understanding how to implement it because there are so many examples out there and they all seem different. Obviously i'm pretty new to Obj-c.All of my views are presented(except the first) with the presentmodalview method, and they each have pretty much the same button menu that presents other views modally, and you can just switch between them. I probably have gone wrong in that I programmed it exactly how it appears visually. i.e. I think I could have used 1 or 2 view controllers instead of 9.
Hippocrates
See the code sample that I've added to my answer...
Michael Kessler