Bog
Bog

Reputation: 37

Swift IOS: several buttons in one view controller, control the image view display in another view controller

I just want to use 30 buttons in one view controller(#1), and when I click one of them(e.g. Button1), the screen can jump to another view controller(2), and display picture1. When I go back to view controller(#1), and click Button 2, it can jump to view controller(#2) and display picture 2...etc. I can switch two view controller using segues, but how can I control the content of image view not in the same view controller? Thanks.

Upvotes: 1

Views: 369

Answers (1)

ErasmoOliveira
ErasmoOliveira

Reputation: 1426

You need to use prepareForSegue method to set the variables in other controllers. in your second controller your attribute needs to be a public.

Objective-C

#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"YOURSEGUENAME"]) {

        secondControllerViewController * secController = [[secondControllerViewController alloc] init];
        [secController setImageName:@"imagenameTeste"];
    }

 }

Swift

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "yourSegue") {
        // pass data to next view
    }
}

Upvotes: 2

Related Questions