UIViewController的子类显示黑屏

时间:2012-03-01 08:00:46

标签: objective-c ios5 uiviewcontroller storyboard

我在我的项目中创建了一个UIViewController的子类,并将它链接到一个由“RootViewController”模式推送的View。我绝对没有对派生类进行任何更改,但是当按下“SecondView”时,它每次都变黑。如果我将该视图链接到标准的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

4 个答案:

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

仅供参考,评论也会自动生成。

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

它会起作用。

答案 2 :(得分: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; 
}

答案 3 :(得分:0)

如果你试图在IB的segue中“显示”之前加载视图,你可能想在目标视图控制器中尝试这样的事情。

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