ewizard ewizard - 27 days ago 7
iOS Question

setHidden not working for UIView

I have a subclass of

UIView
called
InvitedView
. It is instantiated in
viewDidLoad
like this:

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


The class itself looks like this:

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


The method where the view is supposed to be shown is in the view controller showing the view. It ends up getting called, I can verify that the log messages happen all the way up until the
setHidden
function:

ViewController.m

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


I would like to know why
invitedView
isn't being shown after
[invitedView setHidden:NO]
.
It gets all the way to
setHidden
, and then nothing happens. I would appreciate any help, thanks in advance.

Answer

In ViewDidLoad, change line to

[invitedView setHidden:NO];

to make sure you can actually see the view (frame is ok, no view above ...)

You might also want to check Xcodes 3D View Debugging

UPDATE FROM POSTER: The only reason that it wasnt showing up, is that invitedView was being instantiated inside an if statement that wasn't being executed. However - shallowThought's idea to switch setHidden to YES started me down a more productive debugging tract, leading to the discovery.

Comments