简体   繁体   中英

Change a UIImageView on another ViewController using a button?

This code would work if the Ball UIImageView was on the same View Controller but Ball is on a separate View Controller so it says:

Use of undeclared identifier 'Ball'

How should I go about having the images change for Ball when the Actions on this View Controller are implemented?

}
-(IBAction)BallSelect2:(id)sender{
    Ball.image = [UIImage imageNamed:@"ball2fire.png"];

}
-(IBAction)BallSelect3:(id)sender{
    Ball.image = [UIImage imageNamed:@"ball70color.png"];

}
-(IBAction)BallSelect4:(id)sender{
    Ball.image = [UIImage imageNamed:@"sawball.png"];

For this you need the shared instance of Ball UIImageView for all.

Create the one singleton class which contains UIImageView object, and add that class as subview to other view controllers.

当您从一个viewController到另一个viewController执行Segue时,可以使用prepareForSegue方法在此方法中设置UIViewImage的出口。

Create a property of UIImage on next view controller where you want to change image and create local object of UIImage on same viewcontroller and pass image name on respective button click

 -(IBAction)buttonActions:(UIButton *)sender
 {
    switch (sender.tag) {
    case 0:
        ball_Image = [UIImage imageNamed:@"ball2fire.png"];
        break;
    case 0:
        ball_Image = [UIImage imageNamed:@"ball70color.png"];
        break;
    case 0:
        ball_Image = [UIImage imageNamed:@"sawball.png"];
        break;

    default:
        break;
   }
 }
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender        {
         NextViewController *objnext = [segue destinationViewController];
         objnext.ballImage =ball_Image.image;
}

and assign that ballImage to your imageview in NextviewController.

Use delegates. Set delegate for

// -- declare this on SecondViewController.h
id delegate;

// -- on FirstViewController after you instantiate SecondViewController set the delegate
    secondViewController.delegate = self;

And then on the SecondViewController you can call.

(FirstViewController*)self.delegate.Ball setImage["your_image"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM