1

I'm using an iCarousel where i display some buttons. But how do I get to another view when the button is tapped?

I have this code already:

    - (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
    }

How do I get to another view when the button is tapped? Can I use the prepareForSegue method here as well?

Thanks for your help!!

4

1 回答 1

1

当然,您只需将 segue 从第一个 viewController 拖到第二个,然后将其命名为“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...行移到这里)

于 2013-08-06T14:22:08.267 回答