如何将数据从viewcontroller传递到tabbarcontroller too navigationcontroller?

时间:2013-09-04 10:23:15

标签: iphone ios objective-c

你是iphone开发的新手,正在使用故事板进行我的项目, 在我的项目中我有登录viewController如果登录成功它将转到tabbarcontroller。在tabbarController中它有三个viewControllers。在tabbarController和三个视图控制器之间我有一个导航控制器。现在问题是我必须从中传递数据 loginviewController到tabBarcontroller到navigationController。我不知道该怎么做请帮帮我..在此先感谢 我使用此代码将数据从登录控制器传递给tabbarcontroller

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    NSString * segueIdentifier = [segue identifier];
    if([segueIdentifier isEqualToString:@"dashboard"]){

        EventdashViewController *dc = [[EventdashViewController alloc] init];
        FeedDashViewController *fc = [[FeedDashViewController alloc]init];
        NewsDashViewController *nc = [[NewsDashViewController alloc]init];
        UITabBarController* tbc = [segue destinationViewController];
        dc = (EventdashViewController *)[[tbc customizableViewControllers] objectAtIndex:0];
        dc.memberid = userid1;
        NSLog(@"%d",dc.memberid);
        fc = (FeedDashViewController *) [[tbc customizableViewControllers]objectAtIndex:1];
        fc.memberid=userid1;
        NSLog(@"%d",fc.memberid);
        nc = (NewsDashViewController *)[[tbc customizableViewControllers]objectAtIndex:2];
        nc.memberid = userid1;
        NSLog(@"%d",nc.memberid);
    }
}

如何将数据从viewcontroller传递到tabbarcontroller到navigationcontroller?

1 个答案:

答案 0 :(得分:0)

首先,看起来你要做的就是将数据传递给导航控制器的根视图控制器(dc,fc和nc),而不是导航控制器,这就是你应该做的事情。问题是,customizableViewControllers将返回导航控制器,而不是它们的根视图控制器。另外,你使用alloc init的三行是错误的,无论如何都没用,因为你重新定义了dc,fc和nc是几行的 - 如果你在故事板中设置了这一切,那么标签栏控制器的内容控制器都是在标签栏控制器时实例化的。你应该拥有的是:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    NSString * segueIdentifier = [segue identifier];
    if([segueIdentifier isEqualToString:@"dashboard"]){

        UITabBarController* tbc = [segue destinationViewController];
        dc = (EventdashViewController *)[(UINavigationController *)tbc.viewControllers[0] topViewController];
        dc.memberid = userid1;
        NSLog(@"%d",dc.memberid);
        fc = (FeedDashViewController *) [(UINavigationController *)tbc.viewControllers[1] topViewController];
        fc.memberid=userid1;
        NSLog(@"%d",fc.memberid);
        nc = (NewsDashViewController *)[(UINavigationController *)tbc.viewControllers[2] topViewController];
        nc.memberid = userid1;
        NSLog(@"%d",nc.memberid);
    }
}
相关问题