2011-12-01 42 views
0

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

这里的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; 
} 
+0

检查Mainwi ndow.xib,你有'TestViewController'吗? – Hisenberg

+0

@iShrey,我想没有xib文件.. – beryllium

+0

我想创建它没有IB的编程,所以我没有创建xib文件。 – lavitanien

回答

2

如果以编程方式创建它,那么你也应该实例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]; 
+0

非常感谢! – lavitanien

3

在我看来,你没有instatiating类

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

希望它可以帮助

您是否创建了
0

。用于testViewController的xib。如果没有,那么你必须在你的testViewController上添加一个子视图。然后尝试。我希望它会起作用。

UIView *testView=[[UIView alloc]initwithFrame:CGRectMake(0,0,320,480)]; 

[testViewController addsubview:testView]; 
[self.window addSubview:testViewController.view]; 
[self.window makeKeyAndVisible]; 
相关问题