且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将对象从另一个视图控制器添加到数组

更新时间:2023-02-01 16:15:32

你做错了什么是你分配了一个新的 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 !

注意:当然,还有很多其他方法,但可能不是***的方法.但是,它应该可以工作.