setHidden不适用于UIView

时间:2016-11-05 16:01:48

标签: ios objective-c iphone uiview subview

我有一个名为UIView的{​​{1}}子类。它在InvitedView中实例化,如下所示:

ViewController.m

viewDidLoad

课程本身如下:

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

应该显示视图的方法是在显示视图的视图控制器中。它最终被调用,我可以验证日志消息一直发生到#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 函数:

ViewController.m

setHidden

我想知道为什么在- (void)doSomethingWithTheNewValueOfFlagForHid { NSLog(@"issettingtheview******"); dispatch_async(dispatch_get_main_queue(), ^(void){ NSLog(@"issettingtheviewmu2******"); [invitedView setHidden:NO]; }); } 之后没有显示invitedView。 它一直到[invitedView setHidden:NO],然后没有任何反应。我要感谢任何帮助,提前谢谢。

2 个答案:

答案 0 :(得分:1)

ViewDidLoad中,将行更改为

[invitedView setHidden:NO];

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

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

答案 1 :(得分:0)

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