2016-10-28 3 views
0

세 개의 탭 표시 줄 컨트롤러가 있습니다. 이러한 탭 표시 줄 컨트롤러에는 다른보기로 이동하는 단추가 있습니다. 이러한 뷰 중 일부는 탭 표시 줄 컨트롤러와 동일한 다른 뷰 컨트롤러에 있지만 일부는 그렇지 않습니다. 이러한 다양한보기는 차례로 다른보기로 이동할 수 있습니다. 실제로 사용자는이 방법으로 새로운 관점을 영원히 계속할 수 있습니다. 세 가지 주요보기는 NewsFeedVC ProfileVC 및 NotificationsVC입니다. 그런 다음 보조 뷰에는 VotesListVC가 포함됩니다. 의도 된 사용법은 사용자가 세 개의 탭 중 하나를 선택하여 스택에서 더 많은 뷰 컨트롤러를 영원히 터뜨릴 수 있다는 것입니다. 사용자는 내비게이션 뒤로 버튼을 클릭하여 원래보기로 이동하거나 다른 탭을 누를 수 있습니다. 다른 탭을 누르면 이전 스택의 뷰 컨트롤러 스택이 해제됩니다. 원래 탭으로 돌아 가면 루트보기 컨트롤러가 나타납니다. 문제는 내 코드가보기 컨트롤러가 마지막인지 판별하는 것을 터무니없이 엉망으로 만들었다는 것입니다. 이 응용 프로그램에서는 Storyboard를 선택하지 않았으므로 모든 탐색을 직접 수행해야합니다. 내 질문은 스택에 무엇이 있는지와이를 제거하는 방법을 알 수있는 올바른 방법은 무엇입니까? 다음은 일부 코드입니다.네비게이션 스택에 여러 호출 추가

//for when the user wants to leave an individual post allViewControllers is an array  
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers]; 

-(void)backTapped{ 
if (_postKey) { 
    ActivityViewController *newVC = [allViewControllers objectAtIndex:1]; 
    [allViewControllers insertObject:newVC atIndex:0]; 
    [allViewControllers removeLastObject]; 
    self.navigationController.viewControllers = allViewControllers; 

    CATransition* transition = [CATransition animation]; 
    transition.duration = 0.2f; 
    transition.type = kCATransitionPush; 
    transition.subtype = kCATransitionFromLeft; 
    [self.navigationController.view.layer addAnimation:transition 
               forKey:kCATransition]; 
    [self.navigationController popViewControllerAnimated:NO]; 
} 
} 


//Here is how I handle leaving the profileVC, I have booleans determining what view it is from. 

-(void)backTapped{ 

if (fromWorldFeed) { 
    WorldFeedViewController *newVC = [allViewControllers objectAtIndex:1]; 
    newVC.retainTablePosition=true; 
    [allViewControllers insertObject:newVC atIndex:0]; 
    [allViewControllers removeLastObject]; 
    self.navigationController.viewControllers = allViewControllers; 

    [self.navigationController popViewControllerAnimated:NO]; 
}else if(_fromActivity){ 
    ActivityViewController *newVC = [allViewControllers objectAtIndex:1]; 
    [allViewControllers insertObject:newVC atIndex:0]; 
    [allViewControllers removeLastObject]; 
    self.navigationController.viewControllers = allViewControllers; 
}else{ 
    VotesListViewController *newVC =[allViewControllers objectAtIndex:1]; 
    [allViewControllers insertObject:newVC atIndex:0]; 
    [allViewControllers removeLastObject]; 
    self.navigationController.viewControllers =allViewControllers; 
} 
CATransition* transition = [CATransition animation]; 
transition.duration = 0.2f; 
transition.type = kCATransitionPush; 
transition.subtype = kCATransitionFromLeft; 
[self.navigationController.view.layer addAnimation:transition 
              forKey:kCATransition]; 
[self.navigationController popViewControllerAnimated:NO]; 
} 

변경된 사항을 알기 위해로드 할 때 탭 표시 줄 인덱스를 저장하고 어떻게 든 이전 탭보기 스택을 해제 할 수 있습니다. 사용자는 대부분 하나 개 또는 두 개의보기로 이동할 수 있다면

-(void)setTabBarIndex{ 
NSInteger tabBarIndex =self.tabBarController.selectedIndex; 
NSString *tabBarStr=[@(tabBarIndex) stringValue]; 
[[NSUserDefaults standardUserDefaults]setObject:tabBarStr forKey:@"tabBarIndex"]; 

}

이 코드는 잘 될 것입니다. 그러나 사용자가 앱을 자세히 살펴볼 때 고려해야 할 문제가 있습니다. 내 응용 프로그램 대리인이이를 관리 할 수있는 방법이 있습니까?

+0

탭당 탐색 컨트롤러가 없는데 왜 탭을 전환 할 때 이전 활성 탭의 탐색 컨트롤러에 루트로 팝업하도록 지시해야합니다. – Paulw11

답변

1

앞서 언급 한 것처럼 UITabBarController에는 세 개의 탭이 있습니다. 먼저 3 개의 UINavigationController를 사용하십시오. NewsFeedVC, ProfileVC 및 NotificationsVC는 세 개의 UINavigationController에 대한 rootViewController입니다. 그냥보기 컨트롤러를 밀어 수있는 새로운 뷰 컨트롤러를 표시하려면

newsFeedNavigationController = [[UINavigationController alloc] initWithRootViewController:yourNewsFeedVCObject]; 
profileNavigationController = [[UINavigationController alloc] initWithRootViewController:yourProfileVCObject]; 
notificationsNavigationController = [[UINavigationController alloc] initWithRootViewController:yourNotificationsVCObject]; 

yourTabBarController.viewControllers = @[newsFeedNavigationController, profileNavigationController, notificationsNavigationController]; 

이제, 각각의 탐색 컨트롤러 스택을 처리합니다.

뉴스 피드 탭을 선택했다고 가정하면 현재 탐색 컨트롤러는 newsFeedNavigationController입니다. 이제 뷰 컨트롤러 A를 그 위에 올려 놓으려고합니다. 해당 탐색 컨트롤러에서보기 컨트롤러 A의 개체를 누르기 만하면됩니다. 해당 탐색 컨트롤러에서 ProfileVC 또는 NotificationVC의 객체를 푸시하려면 TabBarController.selectedIndex 값을 전환/업데이트 할 필요가 없습니다. newsFeedNavigationController에서 ProfileVC 또는 NotificationVC의 객체를 밀어 넣었으므로 해당 탐색 컨트롤러에 남아 있습니다. 이것은 iOS 앱의 정상적인 동작입니다.

보기 컨트롤러의 newsFeedNavigationController, profileNavigationController 및 notificationsNavigationController 스택이 다릅니다. 푸시/팝은 네비게이션 컨트롤러에서 필요에 따라 실행됩니다 (보기 컨트롤러 푸시를 표시하고 다시 팝업으로 표시하려는 경우). 그것들을 혼합 할 필요는 없습니다.

관련 문제