2014-02-07 4 views
1

현재 저는 UISplitViewController가 있습니다. 마스터보기 컨트롤러에 5 개의 행이 있고 탭을 클릭하면 각각의 상세보기가 화면에 배치됩니다. 기본적으로 일종의 메뉴처럼 작동합니다.UISplitView - 인스턴스가 이미 표시되어있는 경우 세부보기의 새 인스턴스를로드하지 못하도록하려면 어떻게해야합니까?

내가 코딩 한 방식은 didSelectRow : 메서드입니다.

여기 그것은 기본적으로 내가 가지고있는 다른 4 행에 대해 동일합니다 샘플 코드

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     if([indexPath row] == 0) 
     { 
      firstviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"firstviewcontroller"]; 

      UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:firstviewcontroller]; 

      NSArray *vcs = [NSArray arrayWithObjects:[self navigationController], nvc, nil]; 

      [[self splitViewController] setViewControllers:vcs]; 

      [[self splitViewController] setDelegate:firstviewcontroller]; 

입니다.

예를 들어 첫 번째 행을 탭하면 firstviewcontroller가 표시됩니다. 첫 번째 행을 다시 탭하면 현재 인스턴스가 새 인스턴스로 바뀝니다.

어떻게 방지합니까? firstviewcontroller (예 : 텍스트 필드 등)의 세부 정보를 채우고 실수로 첫 행을 탭하면 현재 인스턴스가 바뀌고 모든 텍스트 필드는 비어 있고 다시 채워 져야합니다.

나는 또한 viewcontroller 인스턴스를 보존하는 방법을 묻는 것을 좋아한다. 예를 들어 firstviewcontroller에 필요한 데이터를 채운 다음 secondviewcontroller를 표시하기 위해 두 번째 행을 탭합니다. 첫 번째 행을 다시 탭하면 데이터를 채우는 데 이미 완료 한 firstviewcontroller의 인스턴스를 갖고 싶습니다. (설정 앱 작동 방식과 비슷합니다)

답변

2

사용자가해야 할 일은 메뉴 컨트롤러의 콘텐츠보기 컨트롤러에 대한 강력한 참조를 속성으로 유지하는 것입니다.

@private (nonatomic, strong) UINavigationController *navController1; 
@private (nonatomic, strong) UINavigationController *navController2; 
.... 

다음

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([indexPath row] == 0) 
    { 
     if (!self.navController1) { 
      UIViewController *firstviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"firstviewcontroller"]; 
      self.navController1 = [[UINavigationController alloc] initWithRootViewController:firstviewcontroller]; 
     } 
     if ([self splitViewController] viewControllers] lastObject] != self.navController1) { 
      NSArray *vcs = [NSArray arrayWithObjects:[self navigationController], self.navController1, nil]; 
      [[self splitViewController] setViewControllers:vcs]; 
      [[self splitViewController] setDelegate:self.navController1.rootViewController]; 
     } 
    } 
} 

그들이 때마다 다시되지 않습니다 당신의 상태가 유지됩니다 이런 식으로.

+0

그래, 그가 한 말. – NovaJoe

+0

답변 해 주셔서 감사합니다. 그것은 나를 위해 작동합니다. 이미 존재하는 경우 세부보기의 새 인스턴스를 만들지 않으며, 내가 원하는 다른 세부보기로 이동할 때 현재보기를 유지합니다. – aresz

+0

@NovaJoe 차이점은 내 코드가 splitViewController에 푸시 된 뷰 컨트롤러 중 하나의 상태를 유지한다는 것입니다.이 뷰퍼 컨트롤러는 splitViewController에서만 현재 상태를 유지하고 다른 상황을 표시하지 않도록 선택해야합니다. 제어 장치. – jamone

1

현재 클래스의 클래스를 확인할 수 있습니다. 거기에있을 것으로 예상되는 클래스에 대한 세부 정보보기 컨트롤러를 표시합니다.

if([self.splitViewcontroller.viewControllers.lastObject isKindOfClass:[FirstDetailViewController class]]) 
{ 
    //Here you know that the current detail view controller is of the kind of the first master row... 
    [self.splitViewcontroller.viewControllers.lastObject.navigationController popToRootViewControllerAnimated:YES]; //You don't have to do this, just an example. 
} 
else 
{ 
    //Initialize normally 
}