2013-07-08 2 views
2

많은 사람들이 이미보고 한 문제가 있는데 didSelectViewController이 호출되지 않지만 내 경우에 호출되는 경우가 있습니다. 세 개의 탭과 세 개의보기 컨트롤러가 있습니다. 사용자가 두 번째 또는 세 번째 탭을 누를 때마다 일부 코드를 실행해야합니다. 내 SecondViewController 및 ThirdViewController에서 내가 가진 :didSelectViewController가 특정 경우에 호출되지 않음

UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController; 
[tabBarController setDelegate:self]; 

이제 모든 것이 didSelectViewController 두 번째 탭을 누를 때마다 호출되는의 SecondViewController와 함께 잘 작동합니다. 또한 ThirdViewController didSelectViewController에서 세 번째 탭을 누를 때마다 두 번째 막대가 눌려지지 않은 경우에만 호출됩니다. 그래서 FirstViewController와 ThirdViewController 사이를 앞뒤로 전환하면 모든 것이 OK입니다. 하지만 first-> second-> third와 같은 패턴으로 들어가면 didSelectViewController이 ThirdViewController에서 호출되지 않습니다. 또한 첫 번째 -> 세 번째 -> 두 번째 -> 세 번째가 될 때 didSelectViewController은 처음에는 ThirdViewController에서 호출되지만 두 번째는 호출되지 않습니다. 어떤 아이디어?

+0

삭제하기 전에 내 대답에 대한 귀하의 의견을보고 일이 있었는데 몇 가지 힌트와 함께 귀하의 프로젝트에 예제 코드를 통합하는 방법을 사용하여 내 대답을 편집했습니다. – herzbube

답변

5

그것은 당신이 일을 정확히 따라 힘들지만, 내가 이해에서 당신은 SecondViewControllerThirdViewController 사이에 앞뒤로 UITabBarController의 위임을 변경하여 탭 스위치에 대응하고 있습니다.

사실 인 경우이를 수행하는 것이 좋습니다. 대신 다음을 시도해보십시오.

  • 변경하지 않는 대리인을 지정하십시오. 처음에는 앱 델리게이트를 사용할 수는 있지만, 전용 작은 클래스가 있다면 아마 좋을 것입니다. 이제는 변화가없는 대의원이 있으니 tabBarController: didSelectViewController:에 대한 모든 전화를 100 % 받게 될 것입니다.
  • 대리자 인 개체는 SecondViewControllerThirdViewController 인스턴스에 대한 참조가 있어야합니다. Interface Builder로 UI를 디자인하는 경우에는 위임 클래스에 두 개의 IBOutlet을 추가하고 적절한 인스턴스를 콘센트에 연결하여이 작업을 수행 할 수 있습니다.
  • 이제 대리자가 tabBarController: didSelectViewController:을 받으면 어떤 탭을 선택했는지에 따라 SecondViewController 또는 ThirdViewController 중 하나로 간단히 알림을 전달할 수 있습니다.

기본 코드 예제 :

// TabBarControllerDelegate.h file 
@interface TabBarControllerDelegate : NSObject <UITabBarControllerDelegate> 
{ 
} 

@property(nonatomic, retain) IBOutlet SecondViewController* secondViewController; 
@property(nonatomic, retain) IBOutlet ThirdViewController* thirdViewController; 


// TabBarControllerDelegate.m file 
- (void) tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController 
{ 
    if (viewController == self.secondViewController) 
     [self.secondViewController doSomething]; 
    else if (viewController == self.thirdViewController) 
     [self.thirdViewController doSomethingElse]; 
} 

편집

프로젝트에 위의 예제 코드를 통합하는 방법에 대한 몇 가지 힌트 :

  • 추가의 인스턴스 210에.또한 TabBarController
  • TabBarControllerDelegate 인스턴스에 'TabBarControllerdelegate 콘센트에 연결 포함 XIB 파일은
  • ThirdViewController 인스턴스에 TabBarControllerDelegatethirdViewController 콘센트를 연결합니다 SecondViewController 인스턴스에 TabBarControllerDelegatesecondViewController 콘센트에 연결
  • 방법 추가 - (void) doSomething to SecondViewController
  • - (void) doSomethingElse to ThirdViewController
  • SecondViewController에 코드가없고 ThirdViewController 코드가 TabBarController 대리인으로 변경되어 있는지 확인하십시오!

모든 설정하고 모든 것이 잘 작동하면, 당신은 아마 정리에 비트를 원할 것입니다 :

  • 변경 알림 방법 doSomething
  • 만약 분별 뭔가 doSomethingElse의 이름을 당신은 논평에서 토론을 따라 갔고 secondViewControllerthirdViewController 콘센트를 없애기를 원할 수도 있습니다.
+1

+1 대부분 동의하지만 탭 컨트롤러 위임자가 각보기 컨트롤러에 필요한 콘센트가 필요한 이유를 알지 못합니다. 탭 컨트롤러는 잘못된보기 컨트롤러를 선택하지 않을 것이고 선택된보기 컨트롤러를 알려주므로 알려주는 컨트롤러에 메시지를 보내면됩니다 (컨트롤러가 컨트롤러를 구현하는지 먼저 확인한 후 방법, 물론). – Caleb

+0

@ 캐 일프 거기에 요점이 있습니다. 위임자는 두 VC에 똑같은 메시지를 보내는 경우 콘센트가 필요하지 않습니다. 내 코드 예제에서는 두 개의 서로 다른 메시지 인'doSomething'과'doSomethingElse'를 보내기 때문에 두 컨트롤러를 구별해야합니다. @ flouwer : 내 대답을 편집하고 싶다면 알려주세요. – herzbube

+0

나는 본다. 보기 컨트롤러가 주어진 컨트롤러를 선택할 때 수행 할 작업을 결정하는 대신 탭 컨트롤러를 위임하기를 원할 경우가 있습니까? 그 결정이 각 뷰 컨트롤러에 속한 것처럼 보입니다. – Caleb

5

나는 또한이 문제가 있었고 그것에 싫증이났다. 나는 UITabBarController을 하위 클래스로 만들고 다음 메소드를 재정의하기로 결정했습니다. 두 경우 모두 응용 프로그램 시작시 어떤 이유로 든 setSelectedViewController:이 호출되지 않았습니다.

- (void)setSelectedIndex:(NSUInteger)selectedIndex 
{ 
    [super setSelectedIndex:selectedIndex]; 
    // my code 
} 

- (void)setSelectedViewController:(UIViewController *)selectedViewController 
{ 
    [super setSelectedViewController:selectedViewController]; 
    // my code 
} 
0

스토리 보드에서 this tutorial을 통해 파헤 쳤고 UITabBarControllerDelegate을 사용하는 대안을 생각했습니다. UITabBarControllerDelegate을 고수하려면이 대답을 무시하십시오.

먼저 UITabBarController의 하위 클래스를 만들고 MyTabBarController이라고 부릅니다. 스토리 보드 편집기에서 탭 표시 줄 컨트롤러의 "클래스"속성을 변경하여 스토리 보드가 새 클래스를 선택해야합니다.

스토리 보드 편집기에서 MyTabBarController.m

- (void) prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"SecondVC"]) 
    { 
    SecondViewController* secondViewController = (SecondViewController*)segue.destinationViewController; 
    [secondViewController doSomething]; 
    } 
    else if ([segue.identifier isEqualToString:@"ThirdVC"]) 
    { 
    ThirdViewController* thirdViewController = (ThirdViewController*)segue.destinationViewController; 
    [thirdViewController doSomethingElse]; 
    } 
} 

이 코드를 추가, 당신은 지금 SecondViewControllerThirdViewController에 연결이 segues을 선택하고 각각 SEGUE 식별자 "SecondVC"와 "ThirdVC"로 변경.

내가 잘못하지 않았다면 그게 전부입니다.

관련 문제