xCode 在动态创建的 ViewControllers 之间传递数据 [英] xCode pass data between dynamically created ViewControllers

查看:64
本文介绍了xCode 在动态创建的 ViewControllers 之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个基于 JSON 文件动态创建 ViewController 的项目.前几天我问了一个关于如何在这里创建这个的问题

I am trying to set up a project which dynamically creates ViewControllers based off a JSON file. I asked a question the other day about how to create this here

xCode 动态创建 ViewControllers

所以我已经能够动态创建 ViewControllers 并且在更多地使用它之后,我已经很容易地设置它所以当应用程序加载时,它生成在 JSON 中指定多少数量的 ViewControllers.我还可以很容易地在它们之间导航,还可以让每个屏幕从 JSON 中设置按钮、视图等.我只是通过这样做来做到这一点

So I have been able to create the ViewControllers dynamically and after playing around with it more, I have been quite easily set it up so when the app loads, it generates how ever many numbers of ViewControllers is specified in the JSON. I can also navigate between them easy enough and also have each screen set up buttons, views etc from the JSON. I do this simply by doing

NSDictionary *MainJSON = [sJson JSONValue];
NSArray *theArray = [MainJSON valueForKey:@"viewControllers"];

然后遍历数组创建一个 ViewController 实例.

Then loop through theArray creating a ViewController instance.

我的问题是,我希望能够在 ViewController 之间传递数据.所以说例如我在 JSON 中设置它,所以第一个 ViewController 有一个文本字段.用户将输入一些数据,我希望能够对其进行设置,以便当他们导航到下一个屏幕时,该数据会被传送到下一个屏幕.我目前这样切换屏幕

My problem is, I want to be able to pass data between the ViewControllers. So say for example I set it up in the JSON so the first ViewController has a text field. The user will enter some data and I want to be able to set it up so when they navigate to the next screen, that data is carried to the next screen. I currently switch screens like this

-(void)nextScreen
{
    for(int i = 0; i < [vcArray count]; i++)
    {
        int iID = i+1;
        if([self.navigationController.viewControllers count] == iID)
        {
            ViewController *theVC = [vcArray objectAtIndex:i];

            [self.navigationController pushViewController:theVC animated:YES];
            break;
        }
    }
}

现在通常每个 ViewController 都会有一个类,因此在我推送的 ViewController 中,我可以在该类中执行属性并合成 NSString,因此当将数据传递给下一个 ViewController 时,我会这样做.

Now usually each ViewController would have a class, so in the ViewController I am pushing I can do property and synthesise a NSString in that class, so when passing data to the next ViewController, I would just do this.

vc.theString = textField.text

但是当我动态生成这些 ViewController 时,我不能这样做,因为我只使用了一个 .h 和 .m 文件,并且我生成的每个 ViewController 都使用相同的 XIB 文件.有人对我如何在此设置中传递数据有任何想法吗?

But as I am dynamically generating these ViewControllers, I can't do this as I am only using one .h and .m file and each ViewController I generate is using the same XIB file. Does anyone have any ideas of how I could go about passing data in this set up?

推荐答案

如果您的问题是同一个视图控制器需要不同的 xib,您可以在主 iOS 应用程序中提供所有这些,稍后通过在 JSON 中传递 xib 名称您可以实例化该 xib 并分配给控制器.

If your problem is that you need different xib for same view controller, you can supply all of them in the main iOS app, later by passing xib name in JSON you can instantiate that xib and assign to the controller.

如果您还需要调用不知道名称的 UIView 对象,您也可以随时使用存储在 JSON 中的标签属性.也许您还可以存储 UIView 类型,例如 'UILabel'、'UITextView'、ecc... 并为值调用适当的 setter.

If you also need to recall UIView objects you don't know by name, you can always use the tag attribute stored in JSON as well. Maybe you can also store the UIView type, such as 'UILabel', 'UITextView', ecc... and call appropriate setter for value.

这篇关于xCode 在动态创建的 ViewControllers 之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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