Edward Beringuela Edward Beringuela - 28 days ago 11
iOS Question

Clicking TabBarButton from the sideNavController REFrostedViewController

I was wondering if it is possible to click ButtonA/TabBarButton that is on Controller2, from Controller1 programmatically.

My Controller1 acts as a sideNav, so what I'm actually doing is when I click a certain link, First, I close the sideNav, Second, try to click the second tab item. But it doesn't seem to work.

Here's my sample code in trying to click the tab.

EDIT : Here is my code>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath isEqual:[tableView indexPathForCell:self.cellToClick]])
{
//closes sideNav
[self hideMenuViewController];

//Doesn't go to tab2
FirstController *controller1 = [FirstController new];
controller1.tabBarController.selectedViewController = [controller1.tabBarController.viewControllers objectAtIndex:1];
}
}


To make it clear, I have two buttons in my sideNav, that needs to act as the two tabBarButtons of my tabBarController.

Example, I clicked the first button of my sideNav, it will close the sideNav then click the first Tab of my tabBarController, and so on.

Any idea what's going on here?

EDIT : Tried doing this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath isEqual:[tableView indexPathForCell:self.cellToClick]])
{
//closes sideNav
[self hideMenuViewController];

//Doesn't go to tab2


FirstController *controller1 = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstController"];
controller1.tabBarController.selectedViewController = [controller1.tabBarController.viewControllers objectAtIndex:1];
}
}


ANSWER

if ([indexPath isEqual:[tableView indexPathForCell:self.homeCell]])
{
[self.frostedViewController hideSideNavController];

TabBarViewController *hmtvc = [((UINavigationController*)self.frostedViewController.contentViewController).viewControllers objectAtIndex:0];
hmtvc.selectedViewController = [hmtvc.viewControllers objectAtIndex:0];

}


I already have an instance of my NavigationController THAT is embedded in my TabBarController. The real problem is that I thought at first the I don't have an instance yet. but I have. It is on the third party that I'm using named REFrostedViewController. It holds the instance of the NavigationController.

The frostedViewController.contentViewController holds the NavigationController embeded in the TabBarController, now I just need to access the TabBarController using the NavController.

I did it using :

[((UINavigationController*)self.frostedViewController.contentViewController).viewControllers objectAtIndex:0];


then now I have access to the instance of my tabBarController I just selected the tab that I want, in my case the second tab so I used.

hmtvc.selectedViewController = [hmtvc.viewControllers objectAtIndex:0];

Answer

You think that your firstViewController is already created, so that only you can move tabs and every operation you can do.

Now on tableView did select row at indexpath, you are again creating a new ViewController object, That is not good.

What is already there and you are created a new object.

That is not appropriate.

If you understand this much I can explain you more.

You need to find the tabBarController's reference in your app lifecycle.

That is how tabBarControlelr is associated with self.frostedViewController and then you can select the index os=f a tabBarController.