I have a MultiView app and I have some memory concerns and I would appreciate some advice.. I have an application which initially loads a switch controller which enables the user to change between some views. On some point during the application I want to remove the switchview controller and add another subview to the window..Therefore, I gained access to the delegate of the shared Application and removed the switchview controller and inserted the second one.. I do not understand if this is the right approach to do it and i am afraid that memory leaks will occur since I print the retainCount value of the second controller and it shows 19!!!!!
Below is snapshots of my code.. Is this the right approach? How do I avoid these memory leaks?
Ok in my ApplicationDelegate I have two view controllers which I also set as properties
MyAppDelegate.h
@class SwitchViewController;
@class SecondController;
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
SwitchViewController *switchViewController;
SecondController *secondController;
}
@property (nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
@property (nonatomic, retain) IBOutlet SecondController *secondController;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
in the .m file I add
[self.window addSubview:switchViewController.view];
[self.window makeKeyAndVisible];
notice that I am synthesizing those controllers and release them in the dealloc function
Now here is my problem! In the SwitchViewController.m I want to gain access to the delegate of my App 开发者_StackOverflowremove the current SwitchViewController and place my secondController on, as a subview of the window:
SwitchViewController.m
SecondController *secondController2= [[SecondController alloc] init];
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.switchViewController.view removeFromSuperview];
appDelegate.secondController = secondController2;
[appDelegate.window addSubview:appDelegate.secondController.view];
[secondController2 release];
Here is the question. When I print out [appDelegate.secondController retainCounter] I get 19. Is this the right approach. Do I actually have memory leaks?
Thanks in advance,
Andreas
Your approach looks sound but there are better ways to test than just eyeballing it. Use the Instruments tool that comes with your mac to test if there are leaks.
Also, as a side note, there is a nicer way to do transitions
[UIView transitionFromView:appDelegate.switchViewController.view
toView:appDelegate.secondController.view
duration:1.0
options:UIViewAnimationOptionTransitionNone
completion:nil];
Hope this helps.
精彩评论