setHidden 不适用于 UIView

setHidden not working for UIView

我有一个 UIView 的子class,叫做 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];

class本身是这样的:

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 中,将行更改为

[invitedView setHidden:NO];

确保您能真正看到视图(框架没问题,上面没有视图...)

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

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