0

I am attempting creation and use of a UITabBar in IOS. My code generates a UITabBar but when I click on a tab it crashes with error -

  • (main thread) "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)"

Here is the code, which is called in the view controller's viewDidLoad()

    let v1 : UIView = UIView(frame:self.view.frame);
    let v2 : UIView = UIView(frame:self.view.frame);

    v1.backgroundColor  = UIColor.redColor();
    v2.backgroundColor = UIColor.blueColor();

    let vc1 : UIViewController = UIViewController();
    let vc2 : UIViewController = UIViewController();

    vc1.view = v1;
    vc2.view = v2;

    vc1.title = "View 1";
    vc2.title = "View 2";

    let nav1 : UINavigationController = UINavigationController(rootViewController: vc1);
    let nav2 : UINavigationController = UINavigationController(rootViewController: vc2);

    nav1.delegate = self;
    nav2.delegate = self;

    let tabsArr : [UINavigationController] = [nav1, nav2];

    let main_tab : UITabBarController = UITabBarController();

    main_tab.viewControllers = tabsArr;
    main_tab.delegate = self;

    self.view.addSubview(main_tab.view);

Here is the view from the code above

hopeless

Question

How to get this to work, 100% programmatically? When I click on a tab I would like to respond with a tab change!

Thanks :)

J-Dizzle
  • 4,861
  • 4
  • 40
  • 50
  • put an exception breakpoint and see where it hits after you tap!!! – Teja Nandamuri Dec 17 '15 at 15:34
  • sorry to be such a newb - where and how does an exception breakpoint go :)? – J-Dizzle Dec 17 '15 at 15:35
  • go to breakpoint navigatior on ur xcode left panel. It will be the second one from the right side – Teja Nandamuri Dec 17 '15 at 15:36
  • and then click on + sign at the bottom and add exception breakpoint – Teja Nandamuri Dec 17 '15 at 15:36
  • ok, added! But, and my apologies again... how do you configure to respond to my EXC_BAD_ACCESS? I see the 'Exception', 'Break' and 'Action' fields for my breakpoint but have failed miserably at setting them! My best shot so far is 'All/On Throw/Sound'...! :) – J-Dizzle Dec 17 '15 at 15:43
  • if you want to load a tabbar, try to do it in the app delegte, and set the tabbar controller as rootview controller, instead of doing it in view did load – Teja Nandamuri Dec 17 '15 at 15:45
  • 1
    I'm generally a very encouraging fellow and I'm really glad you're learning iOS coding – welcome! However, I think your code has a few fundamental misunderstandings about the structure of views and view controllers. I can see what you're trying to do, but it's almost entirely backwards and is going to cause you problems almost immediately – not least because there's no view controller containment. If we suggest them, would you be willing to read a couple of tutorials to help you learn how to do this in a better, more maintainable way? – TwoStraws Dec 17 '15 at 15:50
  • Is TwoStraws the same person as Mr.T? :) – J-Dizzle Dec 17 '15 at 16:22

1 Answers1

0

Thanks to Mr.T's help I have resolved it! The key was that the UITabBarController replaces the UIViewController, it does not go inside it!

Here is the final solution in AppDelegate.swift:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds);

    let N : Int = 4;

    for(var i = 0; i < N; i++) {
        print("please?");

        let newView : UIView = UIView();

        let redC   : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
        let greenC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
        let blueC  : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);

        newView.backgroundColor = UIColor(red: redC, green: greenC, blue: blueC, alpha: 1);

        views.append(newView);

        let newViewController : UIViewController = UIViewController();

        newViewController.title = String(format: "View %d", arguments: [i]);

        newViewController.view = newView;

        let newNavC : UINavigationController = UINavigationController(rootViewController: newViewController);

        navs.append(newNavC);
    }

    let main_tab : UITabBarController = UITabBarController();

    main_tab.viewControllers = navs;

    self.window?.rootViewController = main_tab;

    self.window?.makeKeyAndVisible();

    return true;
}

final!

Thank you Mr.T!!!!!!! :)

J-Dizzle
  • 4,861
  • 4
  • 40
  • 50
  • How do you add images to the tabs? For a UITabBarItem this is easy, it's built into the constructor UITabBarItem(title, image, selectedImage). How would you here though? – J-Dizzle Dec 17 '15 at 16:22