0

我正在尝试UIView在屏幕上显示 a 的不同方式......我目前有这段代码,并认为它应该可以将绿色添加UIView到应用程序的窗口中,但是,它没有:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSLog(@"init");
        self.backgroundColor = [UIColor greenColor];
        self.alpha = 1;
    }
    return self;
}

+ (SMLoginView *)sharedView{
    static dispatch_once_t onceToken;
    static SMLoginView *sharedView;
    dispatch_once(&onceToken, ^{
        sharedView = [[self alloc]initWithFrame:[UIScreen mainScreen].bounds];
    });

    return sharedView;
}

+ (void)showLoginView{
    [[self sharedView]show];
}


- (void)show{
    NSLog(@"Show");
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
}
4

1 回答 1

1

Insted,您可以将它添加到被调用的方法中,因为您将它添加到共享类本身中,所以它没有出现。尝试这个

假设你在这个“ViewController.m”类中调用这个共享方法


   - (void)viewDidLoad
     {
         [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.

         SMLoginView *sharedView =  [SMLoginView sharedView]; //you always get a shared view handle that view in this class 

         [self.view addSubview:sharedView]; //add the shared view hear not in the shared class.

    }

希望你明白了:)


于 2013-10-28T06:04:37.480 回答