Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios setHidden不适用于UIView_Ios_Objective C_Iphone_Uiview_Subview - Fatal编程技术网

Ios setHidden不适用于UIView

Ios setHidden不适用于UIView,ios,objective-c,iphone,uiview,subview,Ios,Objective C,Iphone,Uiview,Subview,我有一个子类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

我有一个子类
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];
#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
- (void)doSomethingWithTheNewValueOfFlagForHid {
    NSLog(@"issettingtheview******");
    dispatch_async(dispatch_get_main_queue(), ^(void){
        NSLog(@"issettingtheviewmu2******");
        [invitedView setHidden:NO];
    });
}
该类本身如下所示:

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
- (void)doSomethingWithTheNewValueOfFlagForHid {
    NSLog(@"issettingtheview******");
    dispatch_async(dispatch_get_main_queue(), ^(void){
        NSLog(@"issettingtheviewmu2******");
        [invitedView setHidden:NO];
    });
}
应该显示视图的方法是在显示视图的视图控制器中。它最终被调用,我可以验证日志消息是否一直发生,直到
setHidden
函数:

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];
#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
- (void)doSomethingWithTheNewValueOfFlagForHid {
    NSLog(@"issettingtheview******");
    dispatch_async(dispatch_get_main_queue(), ^(void){
        NSLog(@"issettingtheviewmu2******");
        [invitedView setHidden:NO];
    });
}
我想知道为什么在
[invitedView setHidden:NO]
之后没有显示
invitedView

它一直到达
setHidden
,然后什么也没发生。如果有任何帮助,我将不胜感激。在
viewdiload
中,将行更改为

[invitedView setHidden:NO];
确保您可以实际看到视图(框架正常,上面没有视图…)


您可能还想检查它没有显示的唯一原因是,
invitedView
在未执行的if语句中被实例化。然而-Shallowthough将setHidden切换为YES的想法让我开始了一段更高效的调试过程,最终发现了这一点。

要不要解释一下为什么会这样?还是你是个胆小鬼?你真正的问题有点不清楚。我认为您的意思是,在使用新的lagforhid值调用
dosomething后,您的视图没有按预期显示。如果是这种情况,您是否100%确定视图继承人权限中存在
invitedView
,并且当您取消隐藏它时,它不是零?是的,正如我上面所写的,我实例化了它并将其添加到self.view,然后将其隐藏。我的问题很简单…为什么被邀请的人不出现?谢谢这可能是您需要看到的所有问题,而您真正需要知道的是,我所看到的
是在控制台输出中设置ViewMU2******
,以这种方式隐藏视图。invitedView.alpha=0//用于隐藏,invitedView.alpha=1//用于显示。嘿,syed…谢谢我解决了…但是我有另一个与此代码相关的问题,它没有显示出来,所以它必须是
invitedView.m
的某个东西?将
invitedView=[[invitedView alloc]…
替换为
invitedView=[[UIView alloc]…
来找出答案。非常感谢您的帮助,我修复了它,实际上我在我的viewcontroller中实例化了一个if语句中的视图,但无意中没有调用该语句。感谢调试提示,我相信它们将来会有所帮助。欢迎您提供帮助。也许您想更新或删除该问题,因为它不会被调用对追随者有任何帮助。