ios - 通过 View Controller 、标签栏 Controller 、导航 Controller 和 View Controller 传递数据

标签 ios objective-c uinavigationcontroller storyboard uitabbarcontroller

这可能是非常简单的实现,但我是 iOS 编程的新手,我似乎被卡住了。

所以,基本上,我有一个选项卡式应用程序。我决定除了标签栏之外还需要一个导航栏。为此,我放置了标签栏 Controller ,然后添加了我的 View Controller 并嵌入到每个 View Controller 的导航 Controller 中,然后将其连接到标签栏。

我在 Storyboard中的层次结构看起来有点像这样:

  • View Controller
  • 标签栏 Controller
  • 导航 Controller
  • 查看 Controller
  • 导航 Controller
  • 查看 Controller

  • 我被困在这里的部分是尝试将数据从第一个 View Controller 传递到任何其他 View Controller 时。在添加导航 Controller 之前,我使用 prepareForSegue 方法来传递数据,如下所示:
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"logged"])
        {
            UITabBarController *tabar=segue.destinationViewController;
            SecondViewController *svc=[tabar.viewControllers objectAtIndex:1];
            svc.groupArray = [(NSArray*)sender objectAtIndex:0];
            svc.userArray = [(NSArray*)sender objectAtIndex:1];
            svc.taskArray = [(NSMutableArray*)sender objectAtIndex:2];
            svc.selfArray = [(NSMutableArray*)sender objectAtIndex:3];
            [tabar setSelectedIndex:1];
        }
    }
    

    如您所见,我将数据传递给我的第二个 View Controller 并使用 performSegueWithIdentifier 方法将标签栏索引设置为 1,因为我希望打开第二个页面。所有这一切都工作得很好,直到我将导航 Controller 引入我的代码,因为我想要导航栏。那时一切都崩溃了。如果我尝试按原样运行代码,应用程序会在控制台中崩溃并显示以下输出:

    [UINavigationController setGroupArray:]: unrecognized selector sent to instance 0x7ffa6acec620

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setGroupArray:]: unrecognized selector sent to instance 0x7ffa6acec620'



    我已经尝试过修改代码,但似乎没有任何效果。我只是很困惑,也许正确方向的提示会对我有所帮助。

    最佳答案

    由于您将 View Controller 嵌入到导航 Controller 中,因此它们不再是选项卡栏的直接子级;但是,您将标签栏 Controller 的子级转换为导航 Controller 以外的东西。您想首先获取导航 Controller ,它是标签栏 Controller 的子级,然后获取该导航 Controller 的子级。这将是您的 View Controller 。然后,您可以为此设置数据。

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"logged"])
       {
           UITabBarController *tabar=segue.destinationViewController;
           UINavigationController *navController = [tabbar.viewControllers objectAtIndex:1];
           SecondViewController *svc=[navController.viewControllers objectAtIndex:0];
           svc.groupArray = [(NSArray*)sender objectAtIndex:0];
           svc.userArray = [(NSArray*)sender objectAtIndex:1];
           svc.taskArray = [(NSMutableArray*)sender objectAtIndex:2];
           svc.selfArray = [(NSMutableArray*)sender objectAtIndex:3];
           [tabar setSelectedIndex:1];
       }
    }
    

    关于ios - 通过 View Controller 、标签栏 Controller 、导航 Controller 和 View Controller 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27437892/

    相关文章:

    html - CSS <select> 下拉菜单在 iOS/iPad 浏览器中不显示背景颜色

    ios - UISearchController - 搜索栏在触摸时从包装器中消失

    objective-c - NSMutableDictionary 崩溃并显示 "mutating message sent to immutable object"

    c# - 如何检测控件何时移动到 MonoTouch 中的 View ?

    ios - 获取 iPhone 所在的国家(没有 CLLocationManager)

    ios - ScrollView 中基于平移手势的移动 View Controller

    iphone - 这是 ARC 下的泄漏吗?

    ios - UInavigation Controller 无法从 View Controller 切换到 tableviewcontroller

    ios - 在 presentViewController 不起作用后调用 pushViewController

    ios - 如何检测 iOS 11+ Safari 以及旧版本 Safari 中的隐私浏览?