0

我正在努力将 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但很困惑。

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

4

3 回答 3

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;
    }
}
于 2012-10-16T09:18:01.747 回答
1

首先不要在 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];
            }
        }
    }
于 2012-10-16T09:27:23.513 回答
0

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

于 2012-10-16T10:02:48.663 回答