Objective C UIViewControllers not showing up correctly

I'm stuck on my project. I started out by adding 3 UIViewControllers to the tab controller. Each view controller is simply a different color.

The first view controller automatically shows the correct color, but when I click on the other two tabs, it appears that they are not opening those views, as the color never changes.


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] init];
    self.window.backgroundColor = [UIColor lightGrayColor];
    self.window.frame = [UIScreen mainScreen].bounds;
    self.vc1 = [[ViewController1 alloc] initWithNibName:nil bundle:nil];
    self.vc1.title = @"VC1";
    self.vc2 = [[ViewController1 alloc] initWithNibName:nil bundle:nil];
    self.vc2.title = @"VC2";
    self.vc3 = [[ViewController1 alloc] initWithNibName:nil bundle:nil];
    self.vc3.title = @"VC3";
    self.tabBar = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
    self.tabBar.viewControllers = [NSArray arrayWithObjects: self.vc1, self.vc2, self.vc3, nil];
    self.window.rootViewController = self.tabBar;
    [self.window makeKeyAndVisible];
    return YES;

Open in new window

#import <UIKit/UIKit.h>
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBar;
@property (strong, nonatomic) ViewController1 *vc1;
@property (strong, nonatomic) ViewController1 *vc2;
@property (strong, nonatomic) ViewController1 *vc3;


Open in new window


#import "ViewController1.h"

@interface ViewController1 ()

@property() UIView *myView;


@implementation ViewController1

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myView = [[UIView alloc] initWithFrame:CGRectInset(self.view.bounds, 20.0, 20.0)];
    self.myView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.myView];


Open in new window

ViewController2 and ViewController3 are exactly the same code as ViewController1 except the backgroundColor is different.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I will try and help you.  First I would suggest you just changed the background color of the ViewController instead of adding a UIView.

[self.view setBackgroundColor:[UIColor yellowColor]];

Open in new window

Next put a break point in the view did loads of each of your view controller classes and see which breakpoints are reached.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pzozulkaAuthor Commented:
I changed the background color of the ViewControllers.

I inserted a break point as suggested. Each time I click on one of the tabs, it always goes to the break point inside ViewController1's viewDidLoad method. Even if I click on the tab called "VC2", it still breaks at ViewController1's break point.
pzozulkaAuthor Commented:
Thanks darbid73 for pointing out the issue. I must've been blind. Didn't see that I was using the same class, just calling it by different variable names.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Swift Programming

From novice to tech pro — start learning today.