2013-10-27 43 views
0

我正在試驗在屏幕上顯示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]; 
} 
+0

是什麼調用showLoginView? – futureelite7

回答

1

insted的,美國可以將其添加在調用的方法,因爲ü在共享類本身添加它,這就是WY它我以前不出現。 試試這個

假設ü在這個 「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. 

    } 

希望你得到這個:)