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

时间:2012-10-16 09:04:21

标签: iphone ios xcode nsmutablearray modalviewcontroller

我正在努力将一个NSMutable数组从一个Modal View控制器传递回我来自的视图控制器。

这是我目前的方法:

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但是很困惑。

有没有人有一个很好的教程/清楚简单的方法来做我想要的事情? 谁能告诉我哪里出错了?

3 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:1)

首先不要在secondViewController中创建和分配firstViewController的另一个实例。在第二个ViewController中创建一个属性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];
            }
        }
    }

答案 2 :(得分:0)

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