1

I am working on creating custom Container ViewController class and am able to create 5 sub viewcontrollers and able to move from one child view controller to other child using the following API:

transitionFromViewController:fromViewController
        toViewController:toViewController
            duration:1.0
             options:0
          animations:^{
                              }
          completion:^(BOOL finished) {
                          }];

Child view controllers are occupying part of Parent View Controller. Each time when user swipe I am making previous controller goes off and present controller comes on displayable area.

Now I have buttons on child viewcontrollers and when i click on them I am not getting events or trigger to the Action method of Child View Controller. But Parent View Controller is getting callbacks for its buttons even currently childview is getting displayed. Adding the code snippet below (though it is little bigger, this will help you to analyze if I am doing any mistake).

Parent ViewController:

- (void) handleSwipeGestureLeft:(UISwipeGestureRecognizer *)gesture
{
    if (showPopupValue) {
        return;
    }

    NSInteger index = [arrayOfChildVCs indexOfObject:selectedViewController];
    index = MIN(index+1, [arrayOfChildVCs count]-1);

    UIViewController *newSubViewController = [arrayOfChildVCs objectAtIndex:index];


    endFrame.origin.x = -1024;

    startOfNewVCFrame.origin.x = 1024;

    [self transitionFromViewController:selectedViewController toViewController:newSubViewController];
    selectedViewController = newSubViewController;

}
- (void) handleSwipeGestureRight:(UISwipeGestureRecognizer *)gesture
{
    if (showPopupValue) {
        return;
    }

    NSInteger index = [arrayOfChildVCs indexOfObject:selectedViewController];
    index = MAX(index-1, 0);

    UIViewController *newSubViewController = [arrayOfChildVCs objectAtIndex:index];

    endFrame.origin.x = 1024;

    startOfNewVCFrame.origin.x = -1024;

    [self transitionFromViewController:selectedViewController toViewController:newSubViewController]; 
    selectedViewController = newSubViewController;

}

- (void) populateChildVCList
{


    if (self.currentactivity == EPI_Normal) 
    {
        arrayOfImageNames = [[NSArray alloc]initWithObjects:@"AD_Initiate_SPI_Normal.jpg", @"AD_Initiate_CPI_Normal.jpg", @"AD_Initiate_EAC_Normal.jpg", @"AD_Initiate_PV_Normal.jpg", @"AD_Initiate_EV_Normal.jpg", nil];
    }
    else 
    {
        arrayOfImageNames = [[NSArray alloc]initWithObjects:@"AD_Initiate_SPI_Trigger.jpg", @"AD_Initiate_CPI_Trigger.jpg", @"AD_Initiate_EAC_Trigger.jpg", @"AD_Initiate_PV_Trigger.jpg", @"AD_Initiate_EV_Trigger.jpg", nil];
        isTriggerOn = YES;
    }


    arrayOfChildVCs = [[NSMutableArray alloc] init];

    [arrayOfChildVCs addObject:[[PopupDummyViewController alloc]initWithNibName:@"PopupDummyViewController" bundle:nil]]; // Note: Add one dummy VC to initiate swap

    for (int vcIndex = 0; vcIndex < [arrayOfImageNames count]; vcIndex++) 
    {
        PiSLAComplianceViewController *childPopupController = [[PiSLAComplianceViewController alloc]initWithNibName:@"PiSLAComplianceViewController" bundle:nil];

        childPopupController.popUpImageName = [arrayOfImageNames objectAtIndex:vcIndex];
        childPopupController.imageIndex = vcIndex;
        childPopupController.isTriggerOn = isTriggerOn;

        [arrayOfChildVCs addObject:childPopupController];
    }

    selectedViewController = [arrayOfChildVCs objectAtIndex:0];
    selectedViewController.view.frame = CGRectMake(0, 100, 100, 511); // Took the imageview coordinates
    // Add the dummy view controller to Container View Initially
    [self addChildViewController:selectedViewController];

    [self.view addSubview:selectedViewController.view];

    // notify it that move is done
    [selectedViewController didMoveToParentViewController:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [super viewDidLoad];

    UISwipeGestureRecognizer *swipeGestureRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureRight:)];
    [swipeGestureRecognizerRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [ self.view addGestureRecognizer:swipeGestureRecognizerRight];

    UISwipeGestureRecognizer *swipeGestureRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureLeft:)];
    [swipeGestureRecognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [ self.view addGestureRecognizer:swipeGestureRecognizerLeft];

    endFrame = deliverableimageview.frame;
    startOfNewVCFrame = deliverableimageview.frame;

    [self populateChildVCList]; // To Create list of child view controllers and loads one invisible dummy view controller to be ready for swiping

}

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
    if (fromViewController == toViewController)
    {
        // cannot transition to same
        return;
    }

    // animation setup


    toViewController.view.frame = startOfNewVCFrame;

    // notify
    [fromViewController willMoveToParentViewController:nil];
    [self addChildViewController:toViewController];

    // transition
    [self transitionFromViewController:fromViewController
                      toViewController:toViewController
                              duration:1.0
                               options:0
                            animations:^{
                                toViewController.view.frame = fromViewController.view.frame;
                                fromViewController.view.frame = endFrame;
                            }
                            completion:^(BOOL finished) {
                                [toViewController didMoveToParentViewController:self];
                                [fromViewController removeFromParentViewController];

                            }];



}
halfer
  • 19,824
  • 17
  • 99
  • 186
Srivathsa
  • 606
  • 10
  • 34
  • Won't the parent view controller be in the responder chain of the child view controllers? Therefore just send the button action to the *first responder*. – trojanfoe Feb 11 '13 at 14:47
  • You may be correct. I tried making [toViewController becomeFirstResponder] after loading child. Still not working.. Is it what you are asking.. – Srivathsa Feb 11 '13 at 14:53
  • No. Create the `IBAction` methods in the parent view controller and in the child views, Ctrl-Drag from the button to the *First Responder* and select the appropriate method from the menu that appears. – trojanfoe Feb 11 '13 at 14:54
  • I tried having IBAction in both parent and child. But either of Parent of child are not able to respond to the button click. – Srivathsa Feb 12 '13 at 08:34
  • The parent should be in the responder chain so can you verify that? – trojanfoe Feb 12 '13 at 09:20
  • I guess you are assuming child is derived from parent. No, its parent contains child view controllers. I am new to dev custom container view controller. I don't know container class will be parent of child controllers or how the responder chain in containment. Possibly please share me a piece of code for checking that particular view controller is in responder chain.. – Srivathsa Feb 12 '13 at 09:45
  • See the answer to this question to see how to log the responder chain: http://stackoverflow.com/questions/4241416/how-to-inspect-the-responder-chain – trojanfoe Feb 12 '13 at 10:00
  • Also read this excellent blog article: http://www.cocoanetics.com/2012/09/the-amazing-responder-chain – trojanfoe Feb 12 '13 at 10:08

0 Answers0