2012-03-01 108 views
3

我在我的項目中創建了一個UIViewController的子類,並將它鏈接到由「RootViewController」推送的View。我對派生類完全沒有改變,但是當「SecondView」被推入時,它每次都變黑。如果我將該視圖鏈接到標準的UIViewController類,一切正常?UIViewController的子類顯示黑屏

由於「SecondViewController」派生自UIViewController,我只能猜測問題與alloc/init函數有關,但我不知道從哪裏開始。

如有必要,我可以提供示例代碼,我現在在我面前。

這是所導出的亞類:

#import "SecondViewController.h" 

@interface SecondViewController() 

@end 

@implementation SecondViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; } 

- (void)loadView {} 

- (void)viewDidLoad { 
    [super viewDidLoad]; } 

- (void)viewDidUnload { 
    [super viewDidUnload];} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); } 

@end 

部首:

#import <UIKit/UIKit.h> 

@interface SecondViewController : UIViewController 

@end 
+0

把代碼請好,它會幫助我們很多 – 2012-03-01 08:01:50

+0

完成。希望有所幫助。 – user1242094 2012-03-01 08:27:45

+0

解決了它。 由於意外,我偶然發現了「相關」部分中的一個主題。 我不知道爲什麼,但刪除後刪除 - (void)loadView 函數一切工作正常。 由於子類是由Xcode自動生成的,因此仍然很奇怪。 – user1242094 2012-03-01 09:08:38

回答

2
- (void)loadView 
{ 
    // If you create your views manually, you MUST override this method and use it to create your views. 
    // If you use Interface Builder to create your views, then you must NOT override this method. 
} 

FYI,自動生成評論過。

2

更改此:

- (void)loadView 
{ 
    // If you create your views manually, you MUST override this method and use it to create your views. 
    // If you use Interface Builder to create your views, then you must NOT override this method. 
} 

要這樣:

- (void)loadView 
{ 
    [super loadView]; 
    // If you create your views manually, you MUST override this method and use it to create your views. 
    // If you use Interface Builder to create your views, then you must NOT override this method. 
} 

,它會工作。

1

試試這個

-(void)loadView 
{ 
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; 

    [view setBackgroundColor:[UIColor whiteColor]]; 

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 50, 100, 100)]; 

    [label setText:@"HAI"]; 

    [view addSubview:label]; 

    self.view = view; 
} 
0

如果你想才把它加載視圖「顯示」了來自IB的SEGUE期間,您可能會想嘗試在目標視圖控制器這樣的事情。

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 
[self.view layoutIfNeeded]; 
}