通过JSON动态创建ViewControllers

时间:2014-03-19 13:16:55

标签: ios objective-c json uiviewcontroller

我正在尝试实现此代码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",
}
]
}

我将不胜感激。我一直试图解决这个问题超过一个星期。

2 个答案:

答案 0 :(得分:0)

一种可能性是设置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];
}

答案 1 :(得分:0)

我修改了你的代码,我可以在View中查看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。

enter image description here