2013-05-06 102 views
0

我一直在試圖弄清楚這一點,而不是提出一個解決方案。我有一個視圖控制器與表和第一個單元格表分配給一個名爲「添加朋友」的按鈕。點擊後,它會將您帶到另一個視圖控制器,並在表中包含聯繫人列表。當你點擊一個人時,它會返回到另一個視圖控制器並添加所選人員。這是我迄今爲止所擁有的。從一個視圖控制器添加對象到另一個視圖控制器上的陣列

ContactsViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
FirstViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:@"newVCSegue"]; 

newVC.peopleArray = [[NSMutableArray alloc] init]; 

Person *user = [contactsList objectAtIndex:indexPath.row]; 
NSArray *userKeys = [NSArray arrayWithObjects:@"FirstName", @"LastName", nil]; 
NSArray *userObjects = [NSArray arrayWithObjects:user.firstName, user.lastName, nil]; 
NSDictionary *userDictionary = [NSDictionary dictionaryWithObjects:userObjects forKeys:userKeys]; 
[newVC.peopleArray addObject:userDictionary]; 

[self.navigationController pushViewController:newVC animated:YES]; 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

FirstViewController.h

@property (strong, nonatomic) NSMutableArray *peopleArray; 

FirstViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
//... 
if (indexPath.row == 0) { 
     contactName.text = @"Add Person"; 
     imgView.image = [UIImage imageNamed:@"plus-icon.png"]; 
} else { 
NSString *firstName = [[peopleArray objectAtIndex:(indexPath.row)-1] objectForKey:@"firstName"]; 
NSString *lastName = [[peopleArray objectAtIndex:(indexPath.row)-1] objectForKey:@"lastName"]; 
contactName.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 
} 
return cell; 
} 

這讓我再補充一個朋友,到目前爲止,如果我決定到另一個增加該列表取代了添加的第一位朋友。

+0

後在做什麼你合成peopleArray?如果沒有,綜合它,並使用它作爲self.peopleArray而不是peopleArray ... – 2013-05-06 04:40:06

回答

0

什麼是基本上發生的是每次你選擇一個新的聯繫人,你在第一個視圖控制器重新創建數組,因此它正在取代的東西。理想情況下,您最好儘量避免使用類似Storyboard的FirstViewController,這是非常糟糕的做法,可能會在以後導致各種問題。

我在這種情況下建議創建一個協議(請看委託模式)。這樣一來,你就必須是:

  • 使用水龍頭「添加聯繫人」出現
  • 聯繫人列表,並FirstViewController設置爲代表
  • 用戶點擊聯繫人,將其添加
  • ContactsViewController運籌學爲所選
  • FirstViewController用戶的委託並將該用戶,並駁回視圖控制器

此我通常你會採取的方法,而且實現起來非常簡單。從協議開始

@protocol ContactsDelegate 
-(void) contactsViewController:(ContactsViewController *)vc didSelectContact:(Person *)person; 
@end 

然後,讓你的FirstViewController實現這個協議。爲此,在頭文件中,在名稱後面的尖括號中(<>)添加ContactsDelegate

在FirstViewController的實現中,添加聯繫人委託的新方法。

在你的ContactsViewController中。h文件,添加

@property (nonatomic, assign) NSObject<ContactsDelegate> *delegate; 

然後,當你顯示你的聯繫人視圖控制器,設置委託

userVc.delegate = self; 
[self presentModalViewController:userVc]; 

然後,在用戶視圖控制器didSelectRowAtIndexPath:,只需告知代表您選擇的那個人

[delegate contactsViewController:self didSelectContact:[contactsList objectAtIndex:indexPath.row]]; 

最後,在你的FirstViewController,在您添加的委託方法,我們需要將用戶添加到列表中,而不是重新創建列表

[peopleArray addObject:person]; 

這應該做你:)

+0

你嚇壞了真棒!謝謝!!這工作完美。 :) – Shawn 2013-05-06 22:22:18

+0

真棒,很高興我可以幫助:) – PaReeOhNos 2013-05-06 23:23:35

0

從我所瞭解的情況來看,您每次在ContactsViewController中選擇一個聯繫人時都會實例化一個新的FirstViewController。相反,您應該引用原始的FirstViewController(也許在轉換到ContactsViewController之前保存它),並使用此引用將聯繫人添加到原始數組[original.people addObject:userDict]。只要你確保重新加載表,這應該工作。

相關問題