通过视图控制器,标签栏控制器,导航控制器和视图控制器传递数据 [英] Pass data through view controller, tab bar controller, navigation controller and to view controller

查看:160
本文介绍了通过视图控制器,标签栏控制器,导航控制器和视图控制器传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是非常简单的事情,但我不熟悉iOS编程,我似乎陷入了困境。

This is probably something really simple to achieve, but I am new to programming for iOS and I seem to be stuck.

所以,基本上,我有一个选项卡应用。我决定除了标签栏之外我还想要一个导航栏。为此,我放置了标签栏控制器,然后我添加了我的视图控制器并嵌入到每个视图控制器的导航控制器中,然后连接到标签栏。

So, basically, I have a tabbed application. I decided that I wanted a navigation bar, in addition to the tab bar. To do this, I put the tab bar controller and then I added my view controllers and embedded in a navigation controller for each view controller, which is then connected to the tab bar.

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

My hierarchy in the storyboard looks somewhat like this:


  1. 查看控制器
  1. View Controller

  1. 标签栏控制器
  1. Tab Bar Controller

  1. 导航控制器
  1. Navigation Controller

  1. 查看控制器


  • 导航控制器

  • Navigation Controller


    1. 查看控制器




  • 我被困在这里的部分是在尝试从第一个View Controller传递数据到任何其他View Controller时。在添加导航控制器之前,我使用prepareForSegue方法传递数据,如下所示:

    The part where I am stuck here, is when attempting to pass data from the first View Controller and to any of the other View Controllers. Before adding in the navigation controllers, I was using the prepareForSegue method to pass the data, like so:

    - (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];
        }
    }
    

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

    As you can see, I was passing the data to my second view controller and set the tab bar index to 1 using the performSegueWithIdentifier method, since I wanted the second page to open. All of this was working just fine, until I introduced the Navigation Controllers to my code, since I want navigation bars. That's when everything pretty much broke. If I try to run the code as is, the application crashes with the following output in the console:


    [UINavigationController setGroupArray:]:无法识别选择器发送到
    实例0x7ffa6acec620

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

    ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [UINavigationController setGroupArray:]:发送无法识别的选择器例如0x7ffa6acec620'

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

    我已经尝试了一些代码,但似乎没有什么工作真的。我只是非常困惑,也许正确方向的提示可以帮助我。

    I've tried messing around with the code a bit, but nothing seems to work really. I am just very confused, and maybe a hint in the right direction would help me out a bit.

    推荐答案

    因为你嵌入了你的在导航控制器内查看控制器,它们不再是tabbar的直接子项;但是,您将tabbar控制器的子项转换为导航控制器以外的其他内容。您想首先获取导航控制器,它是tabbar控制器的子控件,然后获取该导航控制器的子控件。这将是您的视图控制器。然后,您可以为此设置数据。

    Since you embedded your view controllers inside navigation controllers they are no longer direct children of the tabbar; however, you are casting the children of the tabbar controller as something other than Navigation controllers. You want to first get the navigation controller, which is a child of the tabbar controller, and then get the child of that navigation controller. This will be your view controller. Then you can set the data for that.

    - (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];
       }
    }
    

    这篇关于通过视图控制器,标签栏控制器,导航控制器和视图控制器传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆