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,因为在视图控制器中,堆栈中的每个视图都保存在内存中,但弹出的视图会被取消分配。