If the "button/checkbox" I have created below is selected by the user on view controller 1, then I would like an image to (appear) display on view controller 2.
if i leave view controller 2 screen and come back to view controller 2 screen, the image should still display as long as the button is still selected on view controller 1.
if the "button" is no longer selected on view controller 1, then the image should no longer be displayed on view controller 2.
Please note that the "button/checkbox" mentioned is being created by the code below. Please help, thank you.
(IBAction)checkButton2:(id)sender {
if (!checked2) {
[_checkBoxButton2 setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
checked2 = YES;
// alert placeholder
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Locked" message:@"this is locked." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
[alert show];
[_checkBoxButton2 setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
checked2 = NO;
}
else if (checked2) {
[_checkBoxButton2 setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
checked2 = NO;
// alert placeholder
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Character selection:" message:@"The box is no longer selected." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
[alert show];
}
}
You should Embed you View into a navigation controller.
When you press Next Screen, perform a segue:
- (IBAction)nextBtn:(id)sender {
[self performSegueWithIdentifier:@"segue" sender:self];
}
before you segue, set a BOOL flag to determine if it should display or not:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"step2Segue"]){
ImageViewController *controller = (ImageViewController *)segue.destinationViewController;
controller.displayImage = checked2;
}
}
Finaly in the ImageViewController add a condition based on the Bool to determine if you should display it or not:
in .h
@interface ImageViewController : ViewController
@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (nonatomic, assign) BOOL displayImage;
@end
In .m
-(void)viewDidLoad{
if (self.displayImage) {
self.image.hidden = YES;
}else{
self.image.hidden = NO;
}
}
One thing at a time.
In the ViewController1 class, you'll have to have an instance of ViewController2 created and made strong in your .h (if you're not using Swift).
@property (nonatomic, strong) ViewController2 *viewController2;
Make sure it's instantiated at the time that you press the button. Within the .h for ViewController 2, you'll have to do something similar with a UIImage and UIImageView or whatever you use to display the picture
@property (nonatomic, strong) UIImage *myImage;
@property (nonatomic, strong) UIImageView *myImageView;
Then, in ViewController1, you'll have to control the events that happen in ViewController2 when you press the button. I would use a BOOL or something else simple to keep track of when the image is supposed to be visible.
BOOL imageShouldBeThere = false;
- (void) myButtonWasPressed: (id) sender
{
if(imageShouldBeThere == false)
{
[viewController2Instance showTheImage];
imageShouldBeThere = true;
}
else
{
[viewController2Instance hideTheImage];
imageShouldBeThere = false;
}
}
This is obviously just a rudimentary template, but it should do the trick.
Working code:
FirstViewController.m
- (IBAction)buttonClicked:(id)sender {
SecondViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
secondViewController.shouldShowImage = YES;
[self presentViewController:secondViewController animated:YES completion:^{
}];
}
SecondViewController.h
@property (nonatomic, assign) BOOL shouldShowImage;
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.shouldShowImage)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
[imageView setImage:[UIImage imageNamed:@"9gag.png"]];
[self.view addSubview:imageView];
}
else
{
//dont show image....
}
}