在容器视图控制器的子视图控制器中处理UIButton

时间:2013-02-11 10:49:52

标签: ios5 uibutton parent-child viewcontroller containment

我正在创建自定义Container ViewController类,并且能够使用以下API创建5个子视图控制器并能够从一个子视图控制器移动到其他子视图:

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

子视图控制器占用父视图控制器的一部分。每次用户滑动时,我都会使之前的控制器熄灭,并且控制器出现在可显示区域。

现在我在子视图控制器上有按钮,当我点击它们时,我没有获得事件或触发子视图控制器的Action方法。但是父视图控制器正在获取其按钮的回调,即使当前正在显示子视图。请问有人请尽快帮忙。添加下面的代码片段(虽然它稍微大一点,这将帮助您分析我是否在做任何错误)。它真的很紧急的请求.. plz plz帮助我..

父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];

                            }];



}

0 个答案:

没有答案