【发布时间】: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