Search code examples
iostabsuitabbarcontrollerselectedindex

Beginner Using Tab Bar and SelectedIndex?


I've built an app 100% in IB without coding. I have little coding background, so please be kind with an answer. :)

I need a way to have a button (that is outside of my tab bar controller) take me to a specific tab in my tab bar controller. When I just do a modal link in IB, it will take me there, but the tab bar controller at the bottom is gone.

How exactly do I do this? (Specifics with an example would be hugely appreciated)

Answer below:

I figured it out on my own...for those that may be looking for a similar solution...

All I did was create a global variable in my initial view controller called globalVariable.

//myview.h
#ifndef Globals_h
#define Globals_h

extern NSInteger globalVariable;

#endif

Populate that view with four buttons with a modal push to my tab bar controller. Each of those buttons has an action that changes the value of the globalVariable.

//myview.m
- (IBAction)button1Action:(id)sender {
    globalVariable = 0;
}
- (IBAction)button2Action:(id)sender {
    globalVariable = 1;
}
//etc etc

Then within the tab bar controller, just set your selectedIndex to the globalVariable within the viewDidLoad

//tabbarcontroller.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.selectedIndex = globalVariable;
}

Solution

  • I figured it out on my own...for those that may be looking for a similar solution...

    All I did was create a global variable in my initial view controller called globalVariable.

    //myview.h
    #ifndef Globals_h
    #define Globals_h
    
    extern NSInteger globalVariable;
    
    #endif
    

    Populate that view with four buttons with a modal push to my tab bar controller. Each of those buttons has an action that changes the value of the globalVariable.

    //myview.m
    - (IBAction)button1Action:(id)sender {
        globalVariable = 0;
    }
    - (IBAction)button2Action:(id)sender {
        globalVariable = 1;
    }
    //etc etc
    

    Then within the tab bar controller, just set your selectedIndex to the globalVariable within the viewDidLoad

    //tabbarcontroller.m
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.selectedIndex = globalVariable;
    }