我开始编写应用程序以显示以下数据结构:
在单词中有子类别的类别,而子类别也可能包含子类别,依此类推。最后一个子类别包含一个项目列表,点击后应显示简短描述。
我正在考虑设计,就像iPhone上的原生设置应用程序一样。一切都包含在导航控制器中,并显示第一个类别。单击时,将显示一个显示子类别的新视图。这将继续,直到单击某个项目并显示说明。
我想知道如何实现这一目标。
NSDictionary
NSDictionary
个NSString
。也许像 JSON 之类的东西然后使用解析器将数据写入NSDictionary
?答案 0 :(得分:0)
您可以以任何您喜欢的格式存储数据,例如XML或JSON就可以了,重点是考虑一个舒适的结构,如index = data
。您可以将数据从之前的VC传递到下一个VC并跟踪其索引
然后您可以按如下方式使用它:
带导航控制器的Root View Controller
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIButton *navButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)navigate:(UIButton *)sender
{
DetailViewController *vc = [DetailViewController new];
vc.prevIndex = 0;
vc.dataString = @"data";
vc.view.backgroundColor = [UIColor whiteColor];
[self.navigationController pushViewController:vc animated:YES];
}
@end
DetailViewController - 用于显示子类别/项目
DetailViewController.h
@interface DetailViewController : UIViewController
@property (nonatomic, assign) NSInteger prevIndex;
@property (nonatomic, strong) NSString *dataString;
@end
DetailViewController.m
#import "DetailViewController.h"
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, 0, 0)];
[button setTitle:[NSString stringWithFormat:@"navigate from %li", (long)_prevIndex] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];
[button sizeToFit];
[self.view addSubview:button];
}
- (void)navigate
{
DetailViewController *vc = [DetailViewController new];
vc.prevIndex = _prevIndex + 1;
vc.dataString = [NSString stringWithFormat:@"data %li", (long)vc.prevIndex];
vc.view.backgroundColor = [UIColor whiteColor];
[self.navigationController pushViewController:vc animated:YES];
}
@end