Gianluca Gianluca - 1 year ago 86
Objective-C Question

How to pass prepareForSegue programmatically an objected created programmatically

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: )
[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?

Answer Source

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