【问题标题】:Passing NSMutableArray from Modal View Controller to Parent View将 NSMutableArray 从模态视图控制器传递到父视图
【发布时间】: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,但很困惑。

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

【问题讨论】:

    标签: iphone ios xcode nsmutablearray modalviewcontroller


    【解决方案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,然后从父视图中获取它。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 2012-06-10
      • 2013-11-20
      相关资源
      最近更新 更多