繁体   English   中英

我的视图控制器没有出现在屏幕上

[英]My view controller doesn't appeared on the screen

我选择“选项卡式应用程序”模板。 然后我添加了CoolViewController,但它没有出现在屏幕上。 怎么了?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
 UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
 self.tabBarController = [[UITabBarController alloc] init];
 self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
 self.window.rootViewController = self.tabBarController;

 CoolViewController *coolVC = [[CoolViewController alloc] init];
 coolVC.view.frame = CGRectMake(0, 0, 320, 200);
 coolVC.view.backgroundColor = [UIColor blackColor];
 [self.window addSubview:coolVC.view];

 [self.window makeKeyAndVisible];
 return YES;
}

@interface CoolViewController : UIViewController
@end

您不应将CoolViewController添加到窗口,而应添加到UITabBarController

您应该以如下形式结束:(注意:我还没有尝试过)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
 UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
 self.tabBarController = [[UITabBarController alloc] init];

 CoolViewController *coolVC = [[CoolViewController alloc] init];
 coolVC.view.frame = CGRectMake(0, 0, 320, 200);
 coolVC.view.backgroundColor = [UIColor blackColor];

 self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,coolVC, nil];
 self.window.rootViewController = self.tabBarController;




 [self.window makeKeyAndVisible];
 return YES;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM