讓我首先說我可以通過prepareForSegue
方法傳遞數據,但是我相信我做這件事的方式並不是最優的,我總是希望提高我的編碼技能。我看到這是否是正確的做法?它有效,但可能「根本上」是錯誤的。通過segue傳遞NSArray
我有一個plist中填充一個UITableView
// Find out the path of recipes.plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"law" ofType:@"plist"];
// Load the file content and read the data into arrays
self.dataArray = [NSArray arrayWithContentsOfFile:path];
//Sort the array by section
self.sortedArray = [self.dataArray sortedArrayUsingDescriptors:@[
[NSSortDescriptor sortDescriptorWithKey:@"Section" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"Title" ascending:YES]]];
//Title
self.namesArray = [self.sortedArray valueForKeyPath:@"Title"];
self.sectionArray = [self.dataArray valueForKeyPath:@"Section"];
//Legislation
self.legislationArray = [self.sortedArray valueForKeyPath:@"Legislation"];
我通過了賽格瑞方法準備傳遞值這麼
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"detailAll"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailTableViewController *destViewController = segue.destinationViewController;
destViewController.title = [self.namesArray objectAtIndex:indexPath.row];
destViewController.nameLabel = [self.namesArray objectAtIndex:indexPath.row];
destViewController.legislationLabel = [self.legislationArray objectAtIndex:indexPath.row];
}
}
這工作。但是,有沒有更乾淨的方法來做到這一點?值被傳遞到另一個數組,然後填充到一個詳細視圖UItableview
這是正確的,乾淨的。你爲什麼認爲這是錯的? –
很高興知道,只是覺得價值觀可以一起傳遞,而不是單獨傳遞。也許在一個NSobject中。 – memyselfandmyiphone
這對我來說看起來不錯,但如果詳細信息VC負責編輯這些值,則您需要屬性上的'copy'屬性,以便以後取消的更改不會影響原始值。 – Droppy