IBAction到子视图以检查图像是否被隐藏

时间:2012-04-06 09:17:30

标签: ios xcode ios5 ibaction

我试图通过单击按钮来检查是否隐藏了子视图的图像。日志确实显示但我无法以某种方式获得图像的隐藏状态。

这里出了什么问题?希望你能帮助我!

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];

隐藏了用于检查子视图中图像的IBAction:

-(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... :(");
    }
}

1 个答案:

答案 0 :(得分:2)

这里的问题是,在-(IBAction)switchImageZwart:(id)sender方法中,您创建了SubSlide1Hoofdstuk3的新实例并检查其属性(UIImageViews),而不是检查您在viewDidLoad:上创建的实际UIImageView对象{1}}。您实际想要的是保留对subslide1的引用并进行检查。

聚苯乙烯。由于调用check方法的按钮实际上是subslide1的子视图,因此您可以获得如下参考:

SubSlide1Hoofdstuk3 *switchactie = [sender superView];

编辑:您实际代码的示例:

你的.h文件中的

@property(nonatomic, strong) SubSlide1Hoofdstuk3 *subslide1;
你的.m文件中的

@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... :(");
    }
}