1

i have a loop in which i create buttons with a selector calling a prepareForSegue. I would pass to the next page the image of the button i clicked.

for (i = 1; i <=  22; i++) //22 it's just temporary
{
    //creating button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self
               action:@selector(nextPage: )
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"" forState:UIControlStateNormal];
    button.frame = CGRectMake(X, Y, 100.0, 100.0);
    UIImage *buttonImageNormal = [UIImage imageNamed:@"ambient.png"]; 

    //image obviously will be different and i want to pass that



    [button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
    if (i%3==0 && i==0)
        X = 20;
    if (i%3==0 && i!=0){
        X = 20;
        Y = Y+150;
    }
    if (i%3==1)
        X = 140;
    if (i%3==2)
        X = 260;
    [_scroll addSubview:button];
}

this is the method called by every button

-(void)nextPage:(id)sender {
UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"signSegue"];
[self.navigationController pushViewController: myController animated:YES];
}

and this is the prepareForSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

addController *controller = (addController *)segue.destinationViewController;
}

what i have to pass with controller?

Gianluca
  • 143
  • 1
  • 10

2 Answers2

1

You should not be calling prepareForSegue directly. Instead, you should trigger your segue programmatically, letting the framework take care of calling prepareForSegue in the due course:

[self performSegueWithIdentifier:@"mySegue" sender:self];
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You can do like following

-(void)nextPage:(id)sender {

    UIButton *pressedButton=(UIButton *)sender;
    SecondViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    myController.pressedButtonImageName=pressedButton.currentBackgroundImage;
    [self.navigationController pushViewController: myController animated:YES];
}

in your second VC :

@property (nonatomic,strong) UIImage *pressedButtonImageName;

Here, you dont need to do anything with prepareForSegue method.

MD.
  • 1,157
  • 11
  • 18