Objective c 如何以编程方式传递以编程方式创建的对象

Objective c 如何以编程方式传递以编程方式创建的对象,objective-c,Objective C,我有一个循环,在这个循环中,我创建了一个按钮,其中一个选择器调用了prepareForSegue。我会将我单击的按钮的图像传递到下一页 for (i = 1; i <= 22; i++) //22 it's just temporary { //creating button UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button addTarget:self

我有一个循环,在这个循环中,我创建了一个按钮,其中一个选择器调用了prepareForSegue。我会将我单击的按钮的图像传递到下一页

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];
}
这是准备室

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

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

我必须向控制器传递什么?

您不应该直接调用
prepareforsgue
。相反,您应该让框架在适当的时候调用
prepareforsgue

[self performSegueWithIdentifier:@"mySegue" sender:self];

你可以像下面这样做

-(void)nextPage:(id)sender {

    UIButton *pressedButton=(UIButton *)sender;
    SecondViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    myController.pressedButtonImageName=pressedButton.currentBackgroundImage;
    [self.navigationController pushViewController: myController animated:YES];
}
在你的第二个VC中:

@property (nonatomic,strong) UIImage *pressedButtonImageName;
在这里,您不需要使用
prepareForSegue
方法执行任何操作

@property (nonatomic,strong) UIImage *pressedButtonImageName;