ios - subview 的 IBAction 以检查图像是否被隐藏

标签 ios xcode ios5 ibaction

我正在尝试通过单击按钮来检查 subview 的图像是否被隐藏。日志确实显示,但我无法以某种方式获取图像的隐藏状态。

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

查看加载:

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

在 subview 中检查图像的 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... :(");
    }
}

最佳答案

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

附言。由于调用检查方法的按钮实际上是您的 subslide1 的 subview ,因此您可以获得如下引用:

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

关于ios - subview 的 IBAction 以检查图像是否被隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10041472/

相关文章:

ios - NSUserDafaults 空值?

ios - 隐藏 View 并删除空格

XCode:在 Storyboard编辑器中移动 UI 元素

iphone - Storyboard奇怪的 Controller View 框架起源

iphone - UIWebView加载本 map 片的时滞

objective-c - rangeOfString 很好,但对于我的字符串比较来说并不完美

ios - JSON 解析,嵌套 JSON 结构问题

ios - LaunchScreen.storyboard 和 SwiftGif

ios - UIPinchGestureRecognizer 向内捏 "slower"

iOS:带有 RTF 的警报 View (粗体、URL 等)