【问题标题】:setHidden not working for UIViewsetHidden 不适用于 UIView
【发布时间】:2017-03-19 07:53:19
【问题描述】:

我有一个UIView 的子类,称为InvitedView。它在viewDidLoad 中实例化,如下所示:

ViewController.m

invitedView = [[InvitedView alloc] initWithFrame:CGRectMake(100, 244, 120, 80)];
invitedView.backgroundColor = [UIColor colorWithRed:156.0f/255.0f green:214.0f/255.0f blue:215.0f/255.0f alpha:0.9f];      
[self.view addSubview:invitedView];
[invitedView setHidden:YES];

类本身看起来像这样:

InvitedView.m

#import "InvitedView.h"
#import "AppDelegate.h"
#import "ViewController.h"

@class ViewController;

@interface InvitedView() {
    UIButton *accept;
    UIButton *decline;
    UILabel *question;
    UIView *gray;
    ViewController *myViewController;
}

@end

@implementation InvitedView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        gray = [[UIView alloc] initWithFrame:frame];

        NSString *holduser = [(AppDelegate*)[[UIApplication sharedApplication] delegate] invitedby];

        [self addSubview:gray];

        accept = [[UIButton alloc] init];
        decline = [[UIButton alloc] init];
        question = [[UILabel alloc] init];
        question.text = [[NSString alloc] initWithFormat:@"You have been invited to a group game by %@", holduser];
        question.numberOfLines = 0;
        question.textAlignment = NSTextAlignmentCenter;
        question.textColor = [UIColor colorWithRed:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f];

        accept.backgroundColor = [UIColor clearColor];
        accept.frame = CGRectMake(20, gray.frame.size.height / 2, (gray.frame.size.width / 2) - 10, (gray.frame.size.height / 2) - 20);
        decline.backgroundColor = [UIColor clearColor];
        decline.frame = CGRectMake((gray.frame.size.width / 2) + 10, (gray.frame.size.width / 2) - 20, (gray.frame.size.width / 2) - 20, (gray.frame.size.height / 2) - 20);
        question.frame = CGRectMake(20, 20, gray.frame.size.width, (gray.frame.size.height / 2) - 20);
        [question setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0]];

        [accept addTarget:myViewController action:@selector(acceptInvite) forControlEvents:UIControlEventTouchUpInside];
        [decline addTarget:myViewController action:@selector(declineInvite) forControlEvents:UIControlEventTouchUpInside];
        [gray addSubview:accept];
        [gray addSubview:decline];
        [gray addSubview:question];

    }
    return self;
}

@end

应该显示视图的方法是在显示视图的视图控制器中。它最终被调用,我可以验证日志消息是否一直发生,直到 setHidden 函数:

ViewController.m

- (void)doSomethingWithTheNewValueOfFlagForHid {
    NSLog(@"issettingtheview******");
    dispatch_async(dispatch_get_main_queue(), ^(void){
        NSLog(@"issettingtheviewmu2******");
        [invitedView setHidden:NO];
    });
}

我想知道为什么invitedView[invitedView setHidden:NO] 之后没有显示。 它一直到setHidden,然后什么也没有发生。如有任何帮助,我将不胜感激,在此先感谢。

【问题讨论】:

  • 想解释一下否决票?还是你是个懦夫?
  • 有点不清楚您的实际问题是什么。我认为您是说调用doSomethingWithTheNewValueOfFlagForHid 后视图未按预期显示。如果是这种情况,您是否 100% 确定 invitedView 存在于视图层次结构中,并且在您取消隐藏时不为零?
  • 是的,正如我上面所写的,我实例化了它并将其添加到 self.view 中,然后我将其隐藏了。我的问题很简单...为什么受邀视图不显示?谢谢。这可能是你需要看到的所有问题,而你真正需要知道的只是我在控制台输出中看到的issettingtheviewmu2******
  • 尝试以这种方式隐藏视图。邀请视图.alpha = 0 // 用于隐藏和邀请视图.alpha = 1 // 用于显示。
  • 嘿 syed...谢谢我想通了...但是我在这里遇到了与此代码相关的另一个问题stackoverflow.com/questions/40442370/…

标签: ios objective-c iphone uiview subview


【解决方案1】:

它没有出现的唯一原因是 invitedView 在一个没有被执行的 if 语句中被实例化。但是 - shallowThought 将 setHidden 切换为 YES 的想法让我开始了一个更有成效的调试路径,从而导致了这一发现。

【讨论】:

    【解决方案2】:

    ViewDidLoad 中,将行改为

    [invitedView setHidden:NO];
    

    确保您可以实际看到视图(框架没问题,上面没有视图...)

    您可能还想查看Xcodes 3D View Debugging

    【讨论】:

    • 没有显示出来,所以肯定是invitedView.m?
    • invitedView = [[InvitedView alloc]...替换为invitedView = [[UIView alloc]...以找出答案。
    • 非常感谢您的帮助,我已修复它,我实际上是在我的视图控制器中实例化视图,其中一个 if 语句意外地没有被调用。感谢您的调试提示,我相信它们将来会有所帮助。
    • 不客气,乐于助人。也许您想更新或删除问题,因为它对关注者没有任何帮助。
    猜你喜欢
    • 2013-12-26
    • 2019-10-16
    • 2013-09-21
    • 2016-12-27
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多