Edward Beringuela Edward Beringuela - 1 month ago 11
iOS Question

Clicking other viewControllers button from another controller

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

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.