试图通过类方法实例化UIView

时间:2013-10-27 21:41:04

标签: iphone objective-c uiview

我正在尝试在屏幕上显示UIView的不同方式...我目前有这个代码,并且认为应该可以将绿色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];
}

1 个答案:

答案 0 :(得分: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.

    }
希望你得到这个:)