2013-08-19 2 views
0

나는 많은 매치를 동시에 할 수있는 턴 기반 게임을 가지고있다. 회전이 끝나면 사용자는 "다음 경기로 이동"버튼을 클릭 할 수 있습니다.Reload UIViewController and Dealloc

버튼을 클릭하면 새로운 GameViewController를 만들고 밀어냅니다. 그러나이 작업을 수행 할 때 이전 GameViewController는 dealloc을 수행하지 않고 때때로 새로운 viewController와 상호 작용합니다 (oldView 컨트롤러에서 점수를 높이는 블록이 새 블록의 점수를 높이는 것처럼).

푸시 애니메이션을 원하기 때문에 핵과 청소 대신 새 viewController를 푸시하고이를 다시 사용하려는 이유는 안전상 이유로 깨끗한 상태를 원하기 때문입니다.

그래서 새로운 viewcontroller를 만들고 푸시 할 때, 실제로는 새로운 것이 아니라 오래된 것 같습니다. 당신은 단지 새로운 뷰 컨트롤러를 누르면 사전

+1

NSLog를 dealloc 메소드에서 호출했는지 확인 했습니까? – ahmedalkaff

답변

1

당신은 GameViewController의 새로운 인스턴스를 밀어하지만 네비게이션 컨트롤러의 스택에 저장하기 때문에 기존의 컨트롤러가 파괴되지 않습니다 : 당신은 (이미 다른 곳으로 이전 뷰 컨트롤러를 제거하지 않는 가정) 이런 식으로 뭔가를 시도 할 수 . 잠재적 인 메모리 누출이 있습니다 - 당신은 각 컨트롤러마다 새로운 컨트롤러를 밀어 넣고 절대로 팝하지 마십시오.

UINavigationController.viewControllers 속성을 변경하여 새 컨트롤러 애니메이션이 끝난 후 탐색 스택을 직접 업데이트 할 수 있습니다. (예를 들어,

// I assume you using ARC. 
NSMutableArray *temp = [NSMutableArray array]; 
[temp addObjectsFromArray:self.navigationController.viewControllers] 
[temp removeObjectAtIndex:temp.count - 2]; 
self.navigationController.viewControllers = [temp copy]; 

같은 있지만,이 솔루션은 어쨌든 나에게 좋아 보인다되지 않습니다)

당신은 탐색 컨트롤러없이 좋은 푸시 전환을 사용할 수는 CATransition 클래스에서 찾아보세요.

+0

고마워! 그게 효과가 있었어. 삭제를 위해 지연을 설정해야하고, 새로운 것을로드하게하십시오. – BlackMouse

0

에서

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil]; 
      GameViewController *gameViewController = (GameViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: @"GameViewController"]; 
      gameViewController.matchObject = match; 
      [weakSelf.navigationController pushViewController:gameViewController animated:YES]; 

덕분에 이전 뷰 컨트롤러는 여전히있을 것입니다. 새로운 것이 위에 있기 때문에 그것은 단지 보이지 않을 것입니다.

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle: nil]; 
GameViewController *gameViewController = (GameViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: @"GameViewController"]; 
gameViewController.matchObject = match; 
[weakSelf.navigationController popViewControllerAnimated:NO]; 
[weakSelf.navigationController pushViewController:gameViewController animated:YES]; 
+0

현재 뷰 컨트롤러가이 줄 ['weakSelf.navigationController popViewControllerAnimated : NO];',''strong' 변수를 사용하는 것이 더 좋다고 생각합니다. –

+0

OP가 원하는 것이 아닙니까? 그는 dealloc을 호출하기를 원하기 때문에 그가 파괴되기를 원한다고 가정합니다. – Kevin

+0

'weakSelf.navigationController popViewControllerAnimated : NO]'[weakSelf.navigationController pushViewController : gameViewController animated : YES]'행은'weakSelf'가 nil이되어 액세스 할 수 없으므로 아무런 의미가 없습니다. 'navigationController' –

관련 문제