Link to home
Start Free TrialLog in
Avatar of pzozulka

asked on

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.
Avatar of darbid73
Flag of Germany image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pzozulka


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