如何从iCarousel中的按钮创建segue到另一个视图

时间:2013-08-06 13:53:23

标签: ios button segue icarousel

我正在使用iCarousel,我在其中显示一些按钮。但是,如果点击按钮,我该如何进入另一个视图?

我已经有了这个代码:

    - (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
    {
        return 10;
    }

    - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
    {
        UIButton *button = (UIButton *)view;
        if (button == nil)
        {
            UIImage *image = [UIImage imageNamed:@"page.png"];
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setBackgroundImage:image forState:UIControlStateNormal];
            button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
            [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        }

        return button;
    }

  - (void)buttonTapped:(UIButton *)sender
    {
        NSInteger index = [carousel indexOfItemViewOrSubview:sender];

        //Action
    }

点按按钮时如何进入其他视图?我也可以在这里使用prepareForSegue方法吗?

感谢您的帮助!!

1 个答案:

答案 0 :(得分:1)

当然,您只需要从第一个viewController拖动并创建一个segue到第二个,然后将其命名为“segue0”。

-(void)buttonTapped:(UIButton*)sender
{
    NSInteger *index = [carousel indexOfItemViewOrSubview:sender];
    if(index = 0)
    {
        [self performSegueWithIdentifier:@"segue0", sender:self];
    }    
    //else if etc.., or better yet, ditch the if, do the checking in 'prepare' as explained below
}

-(void)prepareForSegue:(UIStoryboardSegue*) segue sender:(id)sender
{
    YourNextViewController *nextView = [segue destinationViewController];
    //Do stuff to your next view with "nextView"

}

如果他们都去了同一个viewController,但是通过在prepareForSegue中发送不同的数据,那么你可以放弃if-statement,并发送sender而不是self作为发件人[self performSegue..],并检查prepareForSegue..哪个按钮是发件人(或在此处移动NSInteger *index...行)