1

I'm new with iphone development

I created my storyboard with tabbar and tabbaritems with their viewcontroller

but now I would like to access viewcontroller instances from my uitabbarcontroller.m

anybody have any url with code example?

the codes I find are very simple use or tabbar, mostly just the storyboard which doesn't help in anything...

thanks

---solution after help from Muller

on the uitabbarcontroller:(I placed a breakpoint and was able to retrieve the collection of viewcontrollers)

    - (void)viewDidLoad
    {
       NSArray *arr = self.viewControllers;
    }

AmandaSai98b
  • 485
  • 1
  • 7
  • 17

1 Answers1

2

You could use something like

NSArray *array = [tabBarControllerName viewControllers];

This returns an array of all the views within your tabBarController, so if your first view is of class, let's say TestView we could do something like

TestView *tv = (TestView *)[array objectAtIndex:0];

Note that this is off the top of my head, so it may be a little off. Make sure you check out the class reference materials that apple provides: UITabBarController Class Reference

EDIT: You could really just one-line it like this:

TestView *tv = (TestView *)[[tabBarControllerName viewControllers] objectAtIndex:0];

EDIT2: You can access the UITabBarController if it is declared in your AppDelegate by:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
TestView *tv = (TestView *)[[[appDelegate tabBarController] viewControllers] objectAtIndex:0]; 
Alex Muller
  • 1,565
  • 4
  • 23
  • 42
  • tabbar and how controllers comunicate is very confusing for me, where should I put the code to get the array of viewcontrollers? – AmandaSai98b Apr 24 '12 at 23:47
  • Where are you attempting to access your viewControllers from exactly? UITabBarController.m? If so, you should have a UITabBarController object created already (I hope). If so, you can place this where you need to access them in UITabBarController.m – Alex Muller Apr 24 '12 at 23:48
  • The tabBar itself can be thought of as an array, when you select a button in the tabBar, the tabBar executes the view to be loaded onto the screen (this is an easy way to describe it, but is really more technical than this) – Alex Muller Apr 24 '12 at 23:50
  • I got it, I do have the UITabBarController class and two UIViewController inside of it, I created them on the storyboard and associate to the .m file. now I just don't know where to place the code to return the array of viewcontrollers as you shown before – AmandaSai98b Apr 24 '12 at 23:58
  • shoud I place it on - (void)viewDidLoad from UIViewController? the xcode intelisense doesnt show anything... (I just want to debug and see on the watchlist the viewcontrollers returned...) – AmandaSai98b Apr 24 '12 at 23:59
  • You do it where you are trying to access them. Where are you trying to access these views? If your UITabBarController is created in your AppDelegate, refer to my EDIT2. – Alex Muller Apr 25 '12 at 00:00
  • I think I got it, on the viewDidLoad from UITabbarcontroller I return the viewcontroller collection successfully, I feel relieved now... thanks for the help, I masking this as answered and I'm going to post several other doubts in the next 20 minutes here if you want to answer them and increase ur badges thanks again (and editing my question to place the solution for my case) – AmandaSai98b Apr 25 '12 at 00:08
  • Be sure to click mine as the accepted answer please. If you have other questions, you can chat me (if your rep is high enough) – Alex Muller Apr 25 '12 at 00:09