将 NSMutableArray 从模态视图控制器传递到父视图

Posted

技术标签:

【中文标题】将 NSMutableArray 从模态视图控制器传递到父视图【英文标题】:Passing NSMutableArray from Modal View Controller to Parent View 【发布时间】:2012-10-16 09:04:20 【问题描述】:

我正在努力将 NSMutable 数组从模态视图控制器传递回我来自的视图控制器。

这是我目前的方法:

FirstViewController.h
#import "SecondViewController.h"

@property (strong, nonatomic) IBOutlet NSMutableArray *passedRecipientsArray;


FirstViewController.m
@synthesize passedRecipientsArray = _passedRecipientsArray;

- (void)viewDidAppear:(BOOL)animated 
    NSLog(@"passedRecipientsArray: %@", self.passedRecipientsArray);


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    if([[segue identifier] isEqualToString:@"addContact"])
        UINavigationController *nav = [segue destinationViewController];
        SecondViewController *secondViewController = (SecondViewController *)nav.topViewController;
        secondViewController.emailContact = @"TRUE";
    


SecondViewController.h
@property (strong, nonatomic) IBOutlet NSMutableArray *selectedContactsArray;


SecondViewController.m
@synthesize passedRecipientsArray = _passedRecipientsArray;

- (void)closeWindow

    if([self.selectedContactsArray count] != 0)
        NSLog(@"PASS ME: %@", self.selectedContactsArray);

        FirstViewController *firstViewController = [[FirstViewController alloc] init];

        if(firstViewController.passedRecipientsArray == nil) firstViewController.passedRecipientsArray = [[NSMutableArray alloc] init];
        firstViewController.passedRecipientsArray = self.selectedContactsArray;

        [self dismissModalViewControllerAnimated:YES];
    

有没有更好的方法来做到这一点?我试过用这个:How to pass object on modal view's dismissal,但很困惑。

有没有人有一个很好的教程/清晰简单的方法来做我所追求的? 谁能告诉我哪里出错了?

【问题讨论】:

【参考方案1】:

不要在 SecondViewController 中分配 FirstViewController。因为FirstViewController是你的父类。旧的FirstViewController对象在重新分配后会是null

传递FirstViewController实例而不是写

FirstViewController *firstViewController = [[FirstViewController alloc] init];

示例

SecondViewController.h

#import "FirstViewController.h"

FirstViewController *firstViewController;

@property (strong, nonatomic) IBOutlet NSMutableArray *selectedContactsArray;
@property (strong, nonatomic)  FirstViewController *firstViewController;

SecondViewController.m
@synthesize passedRecipientsArray = _passedRecipientsArray;
@synthesize firstViewController;

- (void)closeWindow

    if([self.selectedContactsArray count] != 0)          

                if(self.firstViewController.passedRecipientsArray == nil)
                       self.firstViewController.passedRecipientsArray = self.selectedContactsArray;         

        [self dismissModalViewControllerAnimated:YES];
    

然后将你的 FirstViewController 修改为

SecondViewController *secondViewController;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    if([[segue identifier] isEqualToString:@"addContact"])
        UINavigationController *nav = [segue destinationViewController];
        secondViewController = (SecondViewController *)nav.topViewController;
        secondViewController.emailContact = @"TRUE";
       secondViewController.firstViewController = self;
    

【讨论】:

我现在在@property (strong, nonatomic) FirstViewController *firstViewController; 上遇到错误,说“未知类型名称 FirstViewController”和“具有保留(或强)属性的属性必须是类型”【参考方案2】:

首先不要在 secondViewController 中创建和分配 firstViewController 的另一个实例。而是在 secondViewController 中创建属性FirstViewController *firstViewController 进一步在 secondViewController .m 文件中合成它...

按照修改后的代码进行

FirstViewController.h
#import "SecondViewController.h"

@property (strong, nonatomic) IBOutlet NSMutableArray *passedRecipientsArray;


FirstViewController.m
@synthesize passedRecipientsArray = _passedRecipientsArray;

- (void)viewDidAppear:(BOOL)animated 
    NSLog(@"passedRecipientsArray: %@", self.passedRecipientsArray);


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    if([[segue identifier] isEqualToString:@"addContact"])
        UINavigationController *nav = [segue destinationViewController];
        SecondViewController *secondViewController = (SecondViewController *)nav.topViewController;
        secondViewController.firstViewController = self;  // u should create firstViewController first in secondViewController class making it a property

        secondViewController.emailContact = @"TRUE";
    

然后在 secondViewController 中

     SecondViewController.h
@interface FirstViewController : UIViewController
FirstViewController *firstViewController;

        @property (strong, nonatomic) IBOutlet NSMutableArray *selectedContactsArray;
        @property(nonatomic,strong) FirstViewController *firstViewController;
    SecondViewController.m
     @synthesize passedRecipientsArray = _passedRecipientsArray;
        @synthesize firstViewController
     - (void)closeWindow
            
                if([self.selectedContactsArray count] != 0)
                    NSLog(@"PASS ME: %@", self.selectedContactsArray);



                if(firstViewController.passedRecipientsArray == nil)  

                firstViewController.passedRecipientsArray = [[NSMutableArray alloc] init];
                firstViewController.passedRecipientsArray = self.selectedContactsArray;

                [self dismissModalViewControllerAnimated:YES];
            
        
    

【讨论】:

【参考方案3】:

我只是想知道为什么不将协议添加到模型视图中?您可以在模型视图中设置 NSMutableArray,然后从父视图中获取它。

【讨论】:

@DavidFord 我给你链接教程,跟着它。我学到的协议也是从这里开始的。 How to pass parameters from one view to another view

以上是关于将 NSMutableArray 从模态视图控制器传递到父视图的主要内容,如果未能解决你的问题,请参考以下文章

将 nsmutablearray 复制到另一个视图控制器中的另一个 nsmutablearray 在数组中显示空值

将属性从模态视图控制器传递给父级

从模态视图呈现视图控制器给出错误

将 TextField 条目从模态视图控制器保存到核心数据单独的实体

以编程方式从模态视图控制器切换父选项卡

如何将params从其他视图模态窗体发送到另一个控制器