如何以编程方式将prepareForSegue传递给以编程方式创建的对象

时间:2016-06-09 14:56:27

标签: objective-c

我有一个循环,我用一个调用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)nextPage:(id)sender {
UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"signSegue"];
[self.navigationController pushViewController: myController animated:YES];
}

这是prepareForSegue

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

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

我必须通过控制器传递什么?

2 个答案:

答案 0 :(得分:1)

您不应该直接致电prepareForSegue。相反,您应该trigger your segue programmatically,让框架负责在适当的时候调用prepareForSegue

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

答案 1 :(得分:0)

您可以执行以下操作

-(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方法做任何事情。