0

I have been trying to figure this out for a while and not coming up with a solution. I have a view controller with a table and the first cell of the table is allocated for a button called "Add Friends". When clicked, it takes you to another view controller with a list of contacts in a table. When you click on a person, it goes back to the other view controller and adds the selected person. This is what I have so far.

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;
}

This lets me add one friend so far and if I decided to add another to the list, it replaces the first friend added.

4

2 回答 2

0

据我了解,每次在 ContactsViewController 中选择联系人时,您都会实例化一个新的 FirstViewController。相反,您应该引用原始的 FirstViewController(可能在转换到 ContactsViewController 之前将其保存),并使用此引用将联系人添加到原始数组[original.people addObject:userDict]中。只要您确保重新加载表格,这应该可以工作。

于 2013-05-06T04:44:22.503 回答
0

基本上发生的事情是每次您选择一个新联系人时,您都在第一个视图控制器中重新创建数组,因此它正在替换东西。理想情况下,您还想尝试避免像这样使用情节提要获取 FirstViewController,这是非常糟糕的做法,以后很可能会导致各种问题。

在这种情况下,我建议创建一个协议(查看委托模式)。这样,您将拥有的是:

  • 使用点击“添加联系人”
  • 联系人列表出现,FirstViewController被设置为delegate
  • 用户点击联系人以添加他们
  • 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];

那应该做你所追求的:)

于 2013-05-06T04:44:52.350 回答