Reputation: 380
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!
Upvotes: 1
Views: 2095
Reputation: 76
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
and then pass it in ur code means in ur
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
Upvotes: 0
Reputation: 868
Why not use a NSString *imageName
property to pass the filename to View Controller 2?
Upvotes: 0
Reputation: 8845
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.
Upvotes: 1