2

我有兩個類。讓我們說A,B類。從A類pushviewController被調用,然後會出現B類。那麼這就是問題所在。當我從B類調用popviewcontrolleranimated方法時,它將返回到A,但是隨後這兩個類的viewwillaapar方法被調用。所以任何人都可以告訴我這裏發生了什麼事。我卡住了! 下面是A班。popViewControllerAnimated會影響viewwillappear?

@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 

這裏是B級

@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 
+0

你的意思是B類viewWillAppear在從B類流到A類之後被調用?是B類viewWillDisappear也叫? – sergio 2012-07-27 08:10:03

+0

是的。 viewwillappear在做pop之後被調用。我沒有改變任何有關viewwilldisappear,所以它是默認的。 – 2012-07-27 08:16:37

回答

0

這是正常的。每次出現視圖時都會調用View。如果只想在首次出現視圖時調用方法,請使用-viewdidload,因爲在視圖控制器中,堆棧中的每個視圖都保存在內存中,但彈出的視圖會被取消分配。