通过 JSON 动态创建 ViewController

Posted

技术标签:

【中文标题】通过 JSON 动态创建 ViewController【英文标题】:Dynamically create ViewControllers through JSON 【发布时间】:2014-03-19 13:16:55 【问题描述】:

我正在尝试实现此代码xCode Dynamically create ViewControllers,但我不确定如何正确实现它。

我需要动态创建视图控制器,然后动态地将按钮添加到这些视图控制器,然后最终使这些按钮在单击时转到那些视图控制器。

首先,我创建了一个“单一视图项目”,然后在 viewController.m 文件中,我在 (void)viewDidLoad 之后添加了以下代码。

/// DATA JSON ADDED TO NSMutableDictionary ...
NSMutableDictionary * allContent = [NSJSONSerialization
JSONObjectWithData:allContentData
options:NSJSONReadingMutableContainers
error:&error];
//// GO THROUGH DATA
if( error )

    NSLog(@"%@", [error localizedDescription]);

else 
//// CONTROLLERS
NSArray *xControllers = allContent[@"xControllers"];
NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithCapacity:0];
for ( NSDictionary *theConDetails in xControllers )

UIViewController * vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
[viewControllers addObject:vc];

//// BUTTONS
NSArray *xButtons = allContent[@"xButtons"];
for ( NSDictionary *theButtonsDetails in xButtons )

NSString *xLabel = [NSString stringWithFormat:@"%@",theButtonsDetails[@"Label"]];
xPositionX = [theButtonsDetails[@"PositionX"]intValue];
xPositionY = [theButtonsDetails[@"PositionY"]intValue];
xWidth = [theButtonsDetails[@"Width"]intValue];
xHeight = [theButtonsDetails[@"Height"]intValue];
xZorder = [theButtonsDetails[@"Z_Order"]intValue];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(xcoPositionX, xcoPositionY, xcoWidth, xcoHeight);
[myButton setTitle:xcoLabel forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(HOW DO i CONNECT THE BUTTON TO GO TO VIEW CONTROLLER:) forControlEvents:UIControlEventTouchUpInside];

[self.view insertSubview:myButton atIndex:xcoZorder];

我的 JSON 数据:

    
xControllers: [

id: "1",
Name: "Screen 1",
Is_First: "Yes" <- MAKE THIS THE FIRST VIEW CONTROLLER
,

id: "2",
Name: "Screen 2",
Is_First: "No"

],
xButtons: [

id: "1",
Name: "Next",
Label: "Next",
View: "1", <- DISPLAY ON VIEW CONTROLLER
Link: "2", <- LINK TO VIEW CONTROLLER
PositionX: "0",
PositionY: "0",
Width: "100",
Height: "100",
Z_Order: "56",
,

id: "2",
Name: "Back",
Label: "Back",
View: "2",  <- DISPLAY ON VIEW CONTROLLER
Link: "1", <- LINK TO VIEW CONTROLLER
PositionX: "0",
PositionY: "0",
Width: "100",
Height: "100",
Z_Order: "56",

]

我的帮助将不胜感激。一个多星期以来,我一直在努力解决这个问题。

【问题讨论】:

【参考方案1】:

一种可能是设置Tag属性并使用该属性获取对应的控制器表单数组:

...
...
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(xcoPositionX, xcoPositionY, xcoWidth, xcoHeight);
[myButton setTitle:xcoLabel forState:UIControlStateNormal];
[myButton setTag:[theButtonsDetails[@"id"]intValue]];  // <---- See this line
[myButton addTarget:self action:@selector(someButtonTouchEvent:) forControlEvents:UIControlEventTouchUpInside];
...
...
-(IBAction)someButtonTouchEvent:(id)sender

    [self.navigationController pushViewController:viewControllers[((UIButton*)sender).tag] animated:YES];

【讨论】:

谢谢 NeverHopeless。这听起来是个好主意。 NeverHopeless 我是新手,对不起,我收到有关此 IBAction 代码 viewControllers[((UIButton*)sender).tag 的此错误 - 错误显示“使用未声明的标识符 'viewController':你意思是“UIViewControllers”? 对不起,我解决了这个错误。我没有在 .h 文件中声明数组。 但是现在代码在 [viewControllers addObject:vc] 处中断; 错误是什么?为什么要初始化容量为 0 的数组?尝试使用NSMutableArray *viewControllers = [[NSMutableArray alloc] init]NSMutableArray *viewControllers = [NSMutableArray array],看看你会得到什么..【参考方案2】:

我已经修改了你的代码,我可以在视图中读取 JSON 文件

for (NSDictionary *theButtonsDetails in xButtons )

        NSString * xLabel = [NSString stringWithFormat:@"%@",theButtonsDetails[@"Label"]];
        NSString * xPositionX = [NSString stringWithFormat:@"%@",theButtonsDetails[@"PositionX"]];
        NSString *  xPositionY = [NSString stringWithFormat:@"%@",theButtonsDetails[@"PositionY"]];
        NSString *  xWidth = [NSString stringWithFormat:@"%@",theButtonsDetails[@"Width"]];
        NSString *  xHeight = [NSString stringWithFormat:@"%@",theButtonsDetails[@"Height"]];

    UIButton * myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake([xPositionX doubleValue], [xPositionY doubleValue], [xWidth doubleValue], [xHeight doubleValue]);
    [myButton setTitle:xLabel forState:UIControlStateNormal];
    myButton.backgroundColor = [ UIColor grayColor];
    [myButton setTag:[theButtonsDetails[@"id"]intValue]];
    [myButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview: myButton];

我替换了 NSString 而不是 NSInteger。

【讨论】:

以上是关于通过 JSON 动态创建 ViewController的主要内容,如果未能解决你的问题,请参考以下文章

如何使用动态单元格高度和自动布局以编程方式创建一个非常基本的 UITableView?

通过 json 数据反应原生动态主题

通过 JSON 更新 Angular 动态组件

AngularJS ng-repeat 通过复杂和动态的 JSON 数组

video.js 使用数据库中的 json 创建动态播放列表

使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON.net 的json的序列化与反序列化