我一直在关注有关 iOS 开发的教程 - 特别是向下钻取 UITableViews。我建立了自己的自定义 plist,但我似乎无法让 DetailViewController 填充我的 plist 信息。我真的需要一些帮助,我有点不知所措!
编辑:这里有一些细节......
该应用程序通过 plist 填充的 RootViewController 工作,这是一个 UITableView。当 plist 中没有任何 child 时,它会更改为详细 View :
AppDelegate.m
NSDictionary *tempDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
self.data = tempDict;
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
if(CurrentLevel == 0) { // At the 'root' of the plist
//Initilalize our table data source
NSArray *tempArray = [[NSArray alloc] init];
self.tableDataSource = tempArray;
AppDelegate *AppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource = [AppDelegate.data objectForKey:@"Rows"];
self.navigationItem.title = @"PedalTome";
}
else
self.navigationItem.title = CurrentTitle;
}
稍后...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the dictionary of the selected data source.
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
//Get the children of the present item.
NSArray *Children = [dictionary objectForKey:@"Children"];
if([Children count] == 0) {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
}
else {
//Prepare to tableview.
RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];
//Increment the Current View
rvController.CurrentLevel += 1;
//Set the title;
rvController.CurrentTitle = [dictionary objectForKey:@"Title"];
//Push the new table view on the stack
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = Children;
}
}
我的 DetailViewController.m
是空的,除了占位符 self.navigationController.title
。
如果我理解正确,我需要将信息从 RootViewController
传递到 DetailViewController
- plist 的位置和实现,索引级别(是什么称为)在 plist 中,以及该索引级别内的字符串(在键 Detail 下)。
在这一点上,任何进步都是惊人的进步。提前致谢。
最佳答案
您可以通过在您的 DetailViewController
中设置一个合成属性,然后将您的数据传递给您的 if block 内的它,将您需要的任何信息传递给您的 DetailViewController
。
例如,在您的 DetailViewController.h
中,您将具有以下内容(没有 ARC):
@property (retain, nonatomic) NSDictionary *myAwesomeDictionary;
或者,启用 ARC:
@property (strong, nonatomic) NSDictionary *myAwesomeDictionary;
然后在 DetailViewController.m
中,您将拥有以下内容:
@synthesize myAwesomeDictionary;
然后您可以将代码块更改为以下内容:
if([Children count] == 0) {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
[dvController setMyAwesomeDictionary:dictionary];
[self.navigationController pushViewController:dvController animated:YES];
}
这假设您在上面几行创建的名为 dictionary
的 NSDictionary
是您想要在 DetailViewController
中显示的数据>.
然后在您的 DetailViewController
的 viewDidLoad:
方法中,您可以使用 self.myAwesomeDictionary
访问该字典并执行您需要执行的任何操作
免责声明:
您的代码中有两点似乎违反了 Apple 的代码风格标准:
- 您的
AppDelegate
存储您的模型(您的 plist)。 - Apple 说你不应该让你的AppDelegate
充满全局数据/逻辑。通常,只编写专门属于某个类的代码,在该特定类中。 - 您没有将 plist 解析为自定义对象。 - 这使得编码变得困难,因为您必须不断弄清楚您的通用 Array 和 Dictionary 对象代表什么,并使您的代码对其他人来说完全不可读。
- 你的一些实例变量名是大写的。例如,
NSArray *Children
应该是NSArray *children
而CurrentLevel
应该是currentLevel
。只有类名的首字母大写。
查看 http://jlawr3nc3.github.com - 具体来说,我的 CompanyRecords 示例代码提供了有关如何创建类的信息,FunWithArrays 提供了有关如何将 plist 解析为自定义对象的信息。 MusicLibraryiOS 然后深入研究如何获取 plist,将其解析为自定义对象,然后将其与详细 View 一起显示在 UITableView
中。
关于ios - 充满 plist 信息的 Detail View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9122156/