Search code examples
iosxcodeios5ibaction

IBAction to subview to check if image is hidden


I am trying to check if a image of a subview is hidden or not, by clicking a button. The log does display but i cant get the hidden status of the image somehow.

Whats going wrong here? Hope u can help me!

Viewdidload:

SubSlide1Hoofdstuk3 *subslide1 = [[SubSlide1Hoofdstuk3 alloc] init];
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
subslide1.view.frame = frame;

// This works finaly
UIImageView *zwart = subslide1.imageZwart;
[zwart setImage:[UIImage imageNamed:@"imageblack.jpg"]]; 
[subslide1.b1 addTarget:self action:@selector(switchImageZwart:) forControlEvents:UIControlEventTouchUpInside];

[_scrollView addSubview:subslide1.view];

The IBAction to check the image in subview is hidden:

-(IBAction)switchImageZwart:(id)sender
{
    SubSlide1Hoofdstuk3 *switchactie = [[SubSlide1Hoofdstuk3 alloc] init];
    UIImageView *wit    = switchactie.imageWit;
    UIImageView *zwart  = switchactie.imageZwart;

    if(zwart.hidden == YES) {
        NSLog(@"Image black is hidden!");
    } else if(wit.hidden == YES) {
        NSLog(@"Image white is hidden!");
    } else {
        NSLog(@"Can't say... :(");
    }
}

Solution

  • The problem here is that inside your -(IBAction)switchImageZwart:(id)sender method you create a new instance of SubSlide1Hoofdstuk3 and checking its properties (the UIImageViews) instead of checking the actual UIImageView objects you created on viewDidLoad:. What you want actually is to hold a reference to subslide1 and check that instead.

    Ps. Since the button calling the check method is actually a subview of your subslide1, you could get a reference like:

    SubSlide1Hoofdstuk3 *switchactie = [sender superView];
    

    EDIT: An example on your actual code:

    in your .h file:

    @property(nonatomic, strong) SubSlide1Hoofdstuk3 *subslide1;
    

    in your .m file:

    @synthesize subslide1;
    
    - (void)viewDidLoad
    {
        //...
        self.subslide1 = [[SubSlide1Hoofdstuk3 alloc] init];
        CGRect frame = self.view.frame;
        frame.origin.x = 0;
        frame.origin.y = 0;
        self.subslide1.view.frame = frame;
    
        // This works finaly
        UIImageView *zwart = self.subslide1.imageZwart;
        [zwart setImage:[UIImage imageNamed:@"imageblack.jpg"]]; 
        [self.subslide1.b1 addTarget:self action:@selector(switchImageZwart:) forControlEvents:UIControlEventTouchUpInside];
    
        [_scrollView addSubview:self.subslide1.view];
    
    }
    
    
    -(IBAction)switchImageZwart:(id)sender
    {
        SubSlide1Hoofdstuk3 *switchactie = self.subslide1;
        UIImageView *wit    = switchactie.imageWit;
        UIImageView *zwart  = switchactie.imageZwart;
    
        if(zwart.hidden == YES) {
            NSLog(@"Image black is hidden!");
        } else if(wit.hidden == YES) {
            NSLog(@"Image white is hidden!");
        } else {
            NSLog(@"Can't say... :(");
        }
    }