在与navigationcontroller捆绑在一起的viewcontrollers之间发送对象

时间:2013-09-11 13:41:38

标签: iphone ios xcode

我有两个视图控制器,它们都与navigationcontrollers捆绑在一起。我试图从一个viewcontroller发送一个对象到另一个。我在下表的didselectrow中使用下面的代码:

UIViewController *serviceTable = [self.storyboard instantiateViewControllerWithIdentifier:@"navigationServiceNews"];

    ((TableServiceNews*)serviceTable).service_ID = [ServiceID objectAtIndex:indexPath.row];

    [self.slidingViewController anchorTopViewOffScreenTo:ECRight
        animations:nil onComplete:^{
        CGRect frame = self.slidingViewController.topViewController.view.frame;
        self.slidingViewController.topViewController = serviceTable;
        self.slidingViewController.topViewController.view.frame = frame;
        [self.slidingViewController resetTopView];
    }];

我正在使用的标识符(navigationServiceNews)指向与目标viewcontroller捆绑在一起的navigationcontroller。

使用此标识符时,应用程序崩溃,因为显然instantiateViewControllerWithIdentifier返回一个veiwcontroller而不是一个navigationcontroller。

当我将标识符更改为目标viewcontroller的ID时,每个工作正常但我丢失了标题上的navigationBar,这对我的项目非常重要(这是我决定将viewcontrollers与navigationcontroller捆绑在一起的原因)。 / p>

1 个答案:

答案 0 :(得分:0)

如果“navigationServiceNews”是您分配给导航控制器的标识符,那么instantiateViewControllerWithIdentifier:将实例化导航控制器而不是UIViewController,因此这不是您的问题。您的问题出在下一行 - 我假设serviceID是TableServiceNews上的属性而不是导航控制器,因此您需要使用topViewController来获取该控制器。

    UINavigationController *serviceTableNav = [self.storyboard instantiateViewControllerWithIdentifier:@"navigationServiceNews"];
    TableServiceNews *serviceTable = serviceTableNav.topViewController;

    serviceTable.service_ID = [ServiceID objectAtIndex:indexPath.row];

    [self.slidingViewController anchorTopViewOffScreenTo:ECRight
            animations:nil onComplete:^{
            CGRect frame = self.slidingViewController.topViewController.view.frame;
            self.slidingViewController.topViewController = serviceTable;
            self.slidingViewController.topViewController.view.frame = frame;
            [self.slidingViewController resetTopView];
    }];