簡體   English   中英

將對象從一個View Controller添加到另一個View Controller上的陣列

[英]Add Objects From One View Controller To An Array On Another View Controller

我一直試圖弄清楚這一點,但沒有提出解決方案。 我有一個帶有表的視圖控制器,該表的第一個單元格分配給一個名為“添加朋友”的按鈕。 單擊時,它將帶您到另一個具有表中聯系人列表的視圖控制器。 當您單擊某個人時,它會返回到另一個視圖控制器並添加所選的人。 到目前為止,這就是我所擁有的。

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

這樣,到目前為止我可以添加一個朋友,如果我決定將另一個朋友添加到列表中,它將替換添加的第一個朋友。

據我了解,每次在ContactsViewController中選擇一個聯系人時,您都將實例化一個新的FirstViewController。 相反,您應該引用原始的FirstViewController(可能在過渡到ContactsViewController之前將其保存),並使用此引用將聯系人添加到原始數組[original.people addObject:userDict] 只要您確保重新加載表,這應該工作。

基本上發生的事情是每次您選擇一個新聯系人時,您都在第一個視圖控制器中重新創建數組,因此它正在替換事物。 理想情況下,您也想嘗試避免像這樣使用情節提要獲取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];

那應該做你想要的:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM