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!!

有帮助吗?

解决方案

Sure, you just have to drag and create a segue from the first viewController to the second, and name it "segue0" for instance.

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

}

If they are all going to the same viewController, but by sending different data in prepareForSegue, then you can ditch the if-statement, and send sender instead of self as the sender in [self performSegue..], and check in prepareForSegue.. which button is the sender(or moving your NSInteger *index... line down here)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top