简体   繁体   中英

My view controller doesn't appeared on the screen

I choose Tabbed Application template. Then I added CoolViewController, but it doesn't appeared on the screen. What is wrong?

- (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

You should not add the CoolViewController to the window but instead to the UITabBarController .

You should end up with something like this: (NOTE:I haven't tried it)

- (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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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