0

我有一个从带有 ui 元素的 XML 文件中解析出来的 NSDictionary。我需要从 NSDictionary 元素构建 ui 并且不知道如何开始这样做。有谁知道?谢谢。

<root>
 <view id="1" color="0.8 0.7 0.9 1.0" x="0" y="0" width="320" height="480">
  <view id="2" color="0.9 0.1 0.3 1.0" x="20" y="20" width="280" height="200">
     <textfield id="5" x="10" y="90" width="180" height="40" placeholder="enter text" color="1.0 1.0 1.0 1.0" target_id="6"></textfield>
     <button id="4" color="0.2 0.2 0.2 1.0" x="220" y="90" width="40" height="40" title="push"></button>
  </view>
  <view id="3" color="0.1 0.8 0.3 1.0" x="20" y="240" width="280" height="100">
     <label id="6" color="0.5 0.5 0.5 1.0" x="20" y="20" title="some label..." width="200" height="80"></label>
  </view>
 </view>
</root>
4

1 回答 1

0

应该很直截了当......像:

UIView *firstView = [[UIView alloc]initWithFrame:CGRectMake([dictionary objectForKey:@"x"], [dictionary objectForKey:@"y", [dictionary objectForKey:@"width"], [dictionary objectForKey:@"height"])];
[self.view addSubView:firstView];
UIView *secondView = <same as above>;
[firstView addSubView:secondView];
UITextField *textField = <build same as above from dictionary>;
UIButton *button = <build same as above from dictionary>;
[secondView addSubView: textField];
[secondView addSubView: button];
// and so on...

当然,您可以通过使用一些逻辑将其包装在一个循环中来使其动态化,以根据您的字典确定要构建哪种对象...

于 2013-05-01T15:43:43.673 回答