I would like to switch between UIViews in a correct way.
This is how I currently do it :
- I trigger a method when clicking on a button
[myButton addTarget:self
action:@selector(switchToMySecondView:)
forControlEvents:UIControlEventTouchUpInside];
- In
switchToMySecondView, I allocate my new UIViewController (mySecondViewController is a attribute of the current class) :
MySecondViewController* mySecondView = [[MySecondViewController alloc] initWithNibName:@"SecondViewXib" bundle:nil];
self.mySecondViewController = mySecondView;
[mySecondView release];
- Add some stuff in mySecondViewController...
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
myLabel.text = [aGlobalArray objectAtIndex:[sender tag]];
[mySecondViewController.view addSubView:myLabel];
- Next I display it, using a UIAnimation :
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[mySecondViewController viewWillAppear:YES];
[self.view addSubview:mySecondViewController.view];
[mySecondViewController viewDidAppear:YES];
[UIView commitAnimations];
- Finally, in my second view controller, I use the reverse method to switch back to my first view :
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.40];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
Ok. That works fine. I have no doubt there are memory leaks, but it's currently not my priority.
Here is my problem :
Each time I click on my button in the first view, I add a different UILabel to my second view (using the [sender tag] index in my aGlobalArray).
Each time this UILabel is added to my secondView, it overlays on the old UILabel, so I still can see the both UILabel.
UILabel is just an example. In my app I am also adding UIImages which overlay too.
I tried to write it in my secondView when switching back to my first view :
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[self release];
[UIView commitAnimations];
And my app is suddenly stopped after 2 or 3 switch/switch back, sometimes without ny message in the console, sometimes with a message saying I am releasing something not allocated.
What am I doing wrong ?
Any help would be greatly appreciated !
You shouldn’t be recreating the UILabel in the second view controller. You could either create the label in your MySecondViewController * class and expose it as a property. Then you’d simply do the following whenever you want to switch to the second view:
Conversely, you could set a tag on the UILabel when you first create it and later retrieve the label using viewWithTag.
EDIT: When I say expose as a property, you’d create the label as normal in your interface:
The in the implementation file:
Then you’d create the label only once – i.e. in MySecondViewController ViewDidLoad or instead of your:
you could do: