开发者

Building a data driven UI

开发者 https://www.devze.com 2023-03-30 04:25 出处:网络
I have searched throughout SO on this topic for iPhone and everything points to the WWDC 2010 coverage, so yes I\'m 开发者_运维百科aware of that. But can anyone point me to a more detailed resource fr

I have searched throughout SO on this topic for iPhone and everything points to the WWDC 2010 coverage, so yes I'm 开发者_运维百科aware of that. But can anyone point me to a more detailed resource from where I can learn how to build a robust system for presenting different user interfaces on an app depending on the data that I'm presenting? I am fetching the data in a JSON format and my UI needs to vary depending on what I get out of the JSON parser.

Any books or online resources that give a detailed look into this topic?

Thanks!


I recently had the same issue in one of my apps (navigation style) and the way I solved it is fairly simple.

I had a user_type flag in my JSON response, and depending on that flag I would push a different view controller.

For example, given my JSON response is stored in a NSMutableDictionary called "response"

if ([response objectForKey:@"account_type"] == 1) {
  /*
    initialize user_type 1 viewController
  */
    [self.navigationController pushViewController:userType1ViewController animated:YES];
else if ([response objectForKey:@"account_type"] == 2) {
  /*
    initialize user_type 2 viewController
  */
    [self.navigationController pushViewController:userType2ViewController animated:YES];
}

You can have as many different user_types as you want.

Editing after clarification in comments

You will likely be manually drawing these views.

I would subclass UIView for each of the different question types you have listed (including properties for common elements like question title, choices, etc). Assuming that questions with the same question type will have the same layout.

Then you can cycle through your JSON response (once it's in an array or dictionary) and lay it out.

If it's a multiple choice question, make a new multipleChoiceQuestion view, add its properties, then addSubview to the main feed view. Then, for your next view, you will need to set the frame to be:

nextQuestion.frame = CGRectMake(0, 0 + firstQuestion.frame.size.height, height, width);

This will ensure that your second question is drawn right below the first question.

0

精彩评论

暂无评论...
验证码 换一张
取 消