【发布时间】: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",
}
]
}
我的帮助将不胜感激。一个多星期以来,我一直在努力解决这个问题。
【问题讨论】:
标签: ios objective-c json uiviewcontroller