setHidden不为UIView工作



我有一个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,然后什么也没发生。

ViewDidLoad中,将line改为

[invitedView setHidden:NO];

确保你可以看到视图(frame是ok的,上面没有视图…)

你可能还想检查Xcodes 3D View Debugging

没有显示的唯一原因是invitedView在未执行的if语句中被实例化。然而,shallowThought将setHidden切换为YES的想法让我开始了一个更高效的调试过程,导致了这个发现。

最新更新