views:

293

answers:

2

hi guys ... i have a problem with switch view between 2 views with nib files ! here my code . my first page goes to page 2 ! but at page 2 i cant back to first page ! my app go out .. here is my code :

from page 1 to 2 :

    #import "HafezViewController.h"
#import "GhazaliateHafez.h"


-(IBAction)gh:(id)sender {
    HafezViewController *ghPage = [[HafezViewController alloc] initWithNibName: @"GhazaliateHafez" bundle:nil];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.3];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
    [self.view addSubview:ghPage.view];
    [UIView commitAnimations];
}

^^^^^^^^^ this code works great ! but from page 2 to back :

#import "GhazaliateHafez.h"
#import "HafezViewController.h"

@implementation GhazaliateHafez


-(IBAction)ghtoIndex:(id)sender {
    HafezViewController *back1 = [[HafezViewController alloc] initWithNibName:@"index" bundle:nil];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [self.view addSubview:back1.view];
    [UIView commitAnimations];

}

after i tap the back button my app go to crashing ... whats my problem ? thank you

A: 

Two thoughts that might help you on the way:

I think you might want to run this through Instruments. Are you leaking memory? You're alloc'ing a view each click, then caching it... are you releasing them?

Would it be easier to have two views that exist separate from this, in an array for example, then index the one you want to make active and wrap that into the animation loop above while releasing and hiding the other one? Not quite sure how to make the other view go away on command as release is garbage controlled... but I know there is a way to do it.

And here's a post from earlier that helped me when I was asking how to remove a subview immediately:

myGroovySubview.hidden = YES; hides the view. You could also try to remove it from its superview with [myGroovySubview removeFromSuperview];

If you removed it from its superview, the release call should automatically remove it from memory since the reference count should be zero after that call.

Spanky
to marc : So i must change this line [self.view addSubview:back1.view]; to this ? : [self.view.removeFromSuperview]; ??
Momeks
Start with view 1. Then add subview 2. When you are done with subview 2 remove it from superview. You are left with subview 1. in your code you add view 2 with addSubview..., and you remove view 2 with removeFormSuperview...
Spanky
thank you ... but can you write the correct code ? am Amateur :(
Momeks
+1  A: 

You are adding view2 to view1, and then adding a view1 back to view2. When done with View2, simple call self.view.removeFromSuperview and view 1 will be shown again.

Actually, looking at this again, it seems like you might want to look at presentModalViewController to show view 2.

marcc
So i must change this line [self.view addSubview:back1.view]; to this ? :[self.view.removeFromSuperview];
Momeks