2

I have two classes. lets say A,B Classes. from A class pushviewController is called, then B class will appear. Then here is the problem .when i call popviewcontrolleranimated method from B class it is going back to A, but then both class`s viewwillappear method is being called. so anyone can tell me what is going on in here. i am stuck!. Below is the A class.

@implementation ShakeViewController

- (id) init {
if (self = [super init]) {
    movieName = @"04";
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shake_it.png"]];
    [self.view addSubview:imageView];
    [imageView release];
    nextController = [[OtsugeViewController alloc] init];
}    
return self;
} 

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
const float violence = 1.2;
static BOOL beenhere;
BOOL shake = FALSE;

if (beenhere) return;
beenhere = TRUE;

if (acceleration.x > violence || acceleration.x < (-1* violence))
    shake = TRUE;
if (acceleration.y > violence || acceleration.y < (-1* violence))
    shake = TRUE;
if (acceleration.z > violence || acceleration.z < (-1* violence))
    shake = TRUE;
if (shake && mPlayerPushed) {
    [self playSound:@"suzu"];

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"noVib"] == NO) {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }

    [[UIAccelerometer sharedAccelerometer] setDelegate:nil];
    [self presentModalViewController:mMoviePlayer animated:YES];
    [self play];
    mPlayerPushed = YES;
}

beenhere = false;
}

- (void)toNext {
NSLog(@"ShakeViewController:toNext");
[self.navigationController pushViewController:nextController animated:NO];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"Shake:viewWillAppear");
}

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:0.5];
}

- (void)dealloc {
[super dealloc];
}

@end

here is B class

@implementation OtsugeViewController

- (id) init {
if (self = [super init]) {
    movieName = @"03";
    self.view = [[[OtsugeView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
}
return self;
}

- (void) toNext {
NSLog(@"OtsugeViewController:toNext");
[self.navigationController popViewControllerAnimated:NO];
}

- (void) toToppage
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];        

[self.navigationController popToRootViewControllerAnimated:NO];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Screen touch Otsuge View");
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                          delegate:self 
                                                        cancelButtonTitle:@"Cancel"  destructiveButtonTitle:nil
                                                otherButtonTitles:@"Retry", @"Main Menu", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;

actionSheet.cancelButtonIndex = 0;
[actionSheet showInView:self.view]; 
[actionSheet release];
}

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex
{
switch (buttonIndex) {
    case 0: // Retry
        [self presentModalViewController:mMoviePlayer animated:YES];
        [self play];
        break;
    case 1: // Main Menu
        [self toToppage];
        break;
    case 2: // Cancel
        break;
    default:
        break;
}
}

- (void) viewWillAppear:(BOOL)animated {
mMoviePlayer.moviePlayer.backgroundView.backgroundColor = [UIColor blackColor];
[self playSound:@"taiko_1"];
NSLog(@"Otsuge:viewWillAppear");
[(OtsugeView *)self.view renewImageView];

[super viewWillAppear:animated];
}

- (void) viewDidAppear:(BOOL)animated{
NSLog(@"Otsuge:viewDidAppear");
[super viewDidAppear:animated];
}

- (void) dealloc {
[super dealloc];
}

@end
  • you mean that class B viewWillAppear is called after doing pop from class B to class A? is class B viewWillDisappear also called? – sergio Jul 27 '12 at 08:10
  • yes. viewwillappear is called after doing pop. i didn`t change anything about viewwilldisappear so it is default. – Mungunbat Enkhbayar Jul 27 '12 at 08:16

1 Answers1

0

It's normal. View will appear is called each time a view will appear. If you want to call a method only when the view appears for the first time use -viewdidload because in a view controller each view that is in the stack is kept in memory but those you pop get deallocated.

Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81