视图未加载

时间:2011-08-08 10:24:03

标签: iphone objective-c cocoa-touch

MoviePlayerAppDelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    viewController = [[MoviePlayerViewController alloc] init];
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

MoviePlayerViewController.m

-(void)loadView {
    //Setup the view.
    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
    [[self view] setBackgroundColor:[UIColor grayColor]];
    [[self view] setUserInteractionEnabled:YES];

    //Add play button.
    playButton = [[UIButton alloc] initWithFrame:CGRectMake(53, 212, 214, 36)];
    [playButton setBackgroundImage:[UIImage imageNamed:@"playButton.png"] forState:UIControlStateNormal];
    [playButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [[self view] addSubview:playButton];
}

但不知怎的view没有加载。我在代码中找不到错误。请帮忙解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您可以尝试在头文件中定义UIView,然后将其附加到self.view

MoviePlayerViewController.h
UIView *contentView;

@property (retain, nonatomic) UIView                    *contentView;

MoviePlayerViewController.m

@synthesize contentView;

- loadView {

CGRect screenRect = [[UIScreen mainScreen] applicationFrame];

contentView = [[UIView alloc] initWithFrame:screenRect];
contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

self.view = contentView;
[contentView release];
}

这绝对有效。

答案 1 :(得分:0)

试试这个:

删除代码

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

并按如下所示更改addSubView。

[self.window addSubview:viewController.view];

[self.window makeKeyAndVisible];