问题描述
我有一个 HomeViewController
,它有一个 tableView,其中填充了数组 tableViewArray
(最初为空).当我点击一个 barButton 时,我会模态地转到另一个名为 OutsideViewController
的视图控制器,它有另一个由不同数组填充的 tableView.
I have a HomeViewController
which has a tableView populated with the array tableViewArray
(originally empty). When I tap on a barButton, I segue modally to another View Controller called OutsideViewController
which has another tableView populated by a different array.
我想做的是:
当我点击 OutsideViewController
中的一行时,我想将选定的字符串值添加到 tableViewArray
以便当我回到 HomeViewController
,tableView 在 tableView 中列出了那个新项目.
When I tap on a row in my OutsideViewController
, I would like to add the selected string value to the tableViewArray
so that when I go back to HomeViewController
, the tableView has that new item listed in the tableView.
到目前为止,这是我尝试过的:
So far, this is what I have tried:
在我的 OutsideViewController.m
的 -didSelectRowAtIndexPath
方法中,我有这段代码:
In the -didSelectRowAtIndexPath
method of my OutsideViewController.m
I have this piece of code:
NSString *selectedRow = [outsideArray objectAtIndex:indexPath.row];
NSMutableArray *temporaryArray = [NSMutableArray arrayWithObject:selectedRow];
HomeViewController *homeVC = [[HomeViewController alloc] init];
homeVC.tableViewArray = temporaryArray;
该代码有效,但是当我返回时 HomeViewController
中的 tableView 仍然是空的.我必须重新加载 tableView 数据吗?我这样做对吗?
That code works but the tableView in HomeViewController
is still empty when I return. Do I have to reload the tableView data?Am I doing this right?
这是我在 Storyboard 中设置视图控制器的方式:
This is how I have set up my View Controllers in Storyboard:
HomeViewController -(modal segue)-> Navigation Controller --> OutsideViewController
另外,从OutsideViewController到HomeViewController的返回是通过这行代码完成的:
Also, the return from OutsideViewController to HomeViewController is done by this line of code:
[self dismissViewControllerAnimated:YES completion:^{ }];
推荐答案
你做错了什么是你分配了一个新的 HomeViewController
.我要做的是在您的 OutsideViewController
中保留对您的 HomeViewController
的引用.方法如下.
What you're doing wrong is you're allocationg a new HomeViewController
. What I would do is keeep a reference to your HomeViewController
in your OutsideViewController
. Here is how.
首先,在OutsideViewController.h
中,创建一个属性,像这样:
First, in OutsideViewController.h
, create a property, like this :
@property (nonatomic, weak) HomeViewController *homeVC;
不要忘记在 .h 中添加 @class HomeViewController;
,在 .m 中添加 #import "HomeViewController.h"
Don't forget to add @class HomeViewController;
in your .h, and #import "HomeViewController.h"
in your .m
在HomeViewController
中,像这样实现prepareForSegue:
方法(用你的segue的标识符替换ModalSegueIdentifier):
In HomeViewController
, implement the prepareForSegue:
method like this (replace ModalSegueIdentifier with your segue's identifier) :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ModalSegueIdentifier"]) {
OutsideViewController *modalVC = (OutsideViewController*)segue.destinationViewController;
modalVC.homeVC = self;
}
}
然后,在 OutsideViewController.m
中,而不是这样做:
Then, in OutsideViewController.m
, instead of doing :
HomeViewController *homeVC = [[HomeViewController alloc] init];
homeVC.tableViewArray = temporaryArray;
这样做:
_homeVC.tableViewArray = temporaryArray;
当您离开模态 VC 时,您的 HomeVC 将拥有正确的数组.不要忘记刷新你的 UITableView
!
When you leave your modal VC, your HomeVC will have the correct array. Don't forget to refresh your UITableView
!
注意:当然,还有很多其他方法,但可能不是最好的方法.但是,它应该可以工作.
这篇关于将对象从另一个视图控制器添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!