Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prepareForSegue not called in TabViewController

I have a UITabViewController that is responsible for managing multiple views (and navigation controllers). The views were connected in Storyboard by Ctrl clicking the UITabViewController and selecting "relationship segue" to the other views.

Problem is, the prepareForSegue is not firing at all in the UITabViewController. Nothing else was touched in code. NSLog does not output upon switching views by clicking on the tab bar.

CustomTabViewController.m

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

    NSLog(@"Here..");
    NSLog(@"%@", [segue destinationViewController]);
}

Does anyone know why this is happening? Thanks

like image 285
user3621606 Avatar asked Dec 07 '22 02:12

user3621606


2 Answers

Assuming that you meant UITabBarController instead of UITabViewController, clicking on a UITabBarItem to switch views will not fire prepareForSegue:sender:. However, it will cause tabBarController:didSelectViewController: to fire in your UITabBarControllerDelegate, so you can use that to perform any actions you wished to perform in prepareForSegue:sender:. If you want to reference the view controller that you're navigating to, instead of [segue destinationViewController], you can just reference the viewController parameter directly.

like image 134
Kamaros Avatar answered Dec 23 '22 15:12

Kamaros


Tab bar controllers (UITabBarController objects) do not segue to the view controllers that are contained in their tabs.

As far as I know there is no such thing as a UITabViewController. It pays to use the correct class names when posting a question. At first I misread your question, and thought it said "UITableViewController".(So did @MatteoGobbi, it seems)

like image 30
Duncan C Avatar answered Dec 23 '22 15:12

Duncan C