当应用启动时加载视图



我想创建一个只有一个视图的应用程序(TestViewController.h TestViewController.m)。(没有标签栏,没有导航栏)不知道为什么我启动应用程序后,屏幕完全黑了。似乎应用程序没有成功加载视图?因为如果视图被加载,屏幕应该是白色的。我说的对不对?

这是AppDelegate.h

#import <UIKit/UIKit.h>
@class TestViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIWindow *window;
    TestViewController *testrViewController;   
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) TestViewController *testViewController;
@end

这是AppDelegate.m

#import "AppDelegate.h"
#import "TestViewController.h"
@implementation AppDelegate
@synthesize window = window;
@synthesize testViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self.window addSubview:testViewController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

在我看来,你好像没有在初始化这个类

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // add this line
 testViewController = [[TestViewController alloc] init];
 [.....]
 }

希望有所帮助

如果您以编程方式创建它,那么您还应该实例化window

UIWindow *aWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = aWindow;
[aWindow release];

然后是ViewController

testViewController = [[TestViewController alloc] init];

,然后使其可见

[self.window addSubview:testViewController.view];
    [self.window makeKeyAndVisible];

是否为testViewController创建了.xib ?如果没有,那么你必须在testViewController上添加子视图。然后尝试。我希望它能起作用。

UIView *testView=[[UIView alloc]initwithFrame:CGRectMake(0,0,320,480)];
[testViewController addsubview:testView];
[self.window addSubview:testViewController.view];
[self.window makeKeyAndVisible];

最新更新