简体   繁体   中英

IBAction to set UIImageview image in second View Controller?

I am trying to set up something where the IBAction from view controller 1 would set the ImageView image in View Controller 2's viewDidLoad using tags to differentiate between the buttons pressed.... Something like (not exact code...just hashing it out in my head):

In View Controller 1:

-(IBAction)buttonpressed:(id)sender {

if (button.tag == 1) {

ViewController2ImageView.image = [UIImage imageNamed:@"image1.png"];

}else if (button.tag == 2) {

ViewController2ImageView.image = [UIImage imageNamed:@"image2.png"];
}

In View Controller 2:

-(void)viewDidLoad {

ViewController2ImageView.image = [UIImage imageNamed:@"?.png"];
}

The issue would be how to get the info from the IBAction to pass to the second view controller and having the viewDidLoad method load the correct image.

Ive seen examples converting data to NSString and passing it that way however I was really wondering if there was a better way to go about this.

Ive been able to Google my way through putting a few apps together so far however this has had me scratching my head for a few days now.

Any help or suggested code would be helpful! Even if its just a push in the right direction.

Thanks!

When the button is pressed in viewController1, do something like this:

-(IBAction)buttonpressed:(id)sender {
    if (button.tag == 1) {
        [[NSUserDefaults standardUserDefaults] setObject:@"image1.png" 
            forKey:@"viewController2SelectedImageKey"];
    } else if (button.tag == 2) {
        [[NSUserDefaults standardUserDefaults] setObject:@"image2.png" 
            forKey:@"viewController2SelectedImageKey"];
    } else {
        //set some default string for the image
        [[NSUserDefaults standardUserDefaults] setObject:@"yourDefaultImage.png" 
            forKey:@"viewController2SelectedImageKey"];
    }
}

Then in your viewDidLoad method in viewController2 do something like this :

- (void)viewDidLoad {
    ViewController2ImageView.image = [UIImage imageNamed:[[NSUserDefaults 
        standardUserDefaults] objectForKey:@"viewController2SelectedImageKey"]];
    //other setup
}

This code sets a string to a key in the NSUserDefaults plist (the key I used is @"viewController2SelectedImageKey" but you can use any string you like), which is referenced by viewController2 upon its viewDidLoad method and uses the string as the name of the image file.

为什么不使用NSString *imageName属性将文件名传递给View Controller 2?

Hi You can do simply with many type as below 1)you can write the method

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle )nibBundleOrNil andImageName:(NSString )ImageName;

    write this mehod in the second view & call from the first view and when this method call at that time store this image name in the second local variable like below

    • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle )nibBundleOrNil andImageName:(NSString )ImageName { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization //Local Variable Assignment imageName=ImageName; } return self; }

and then pass it in ur code means in ur

  • (void)viewDidLoad { ViewController2ImageView.image = [UIImage imageNamed:imageName]; }

2)Second type is create imageName Varibale in the secound view and give @property and @synthesize it and than use it at below

 SecoundView *ObjSecoundView=[[ObjSecoundView alloc] initWithNibName:@"SecoundView" bundle:nil];
    ObjSecoundView.imageName=@"Image1.png";//put ur dynamic image name
    [self.navigationController  pushViewController:ObjSecoundView animated:YES];

pass this and use in the viewdid load method as below

  • (void)viewDidLoad { ViewController2ImageView.image = [UIImage imageNamed:imageName]; }

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