将NSMutableArray从模态视图控制器传递到父视图 [英] Passing NSMutableArray from Modal View Controller to Parent View
问题描述
我正在努力将NSMutable数组从Modal View控制器传递回我来自的视图控制器.
I'm struggling to pass a NSMutable array from a Modal View controller back to the view controller I came from.
这是我当前的方法:
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];
}
}
是否有更好的方法?我尝试使用过此方法:如何在模式视图的关闭时传递对象,但非常困惑.
Is there a better way of doing this? I've tried to used this: How to pass object on modal view's dismissal but get very confused.
有人对我要做什么有很好的教程/简单明了的方法吗? 谁能告诉我我要去哪里错了?
Does anyone have a good tutorial/clear easy way of doing what I'm after? Can anyone tell me where I'm going wrong?
推荐答案
首先,不要在secondViewController中创建并分配firstViewController的另一个实例..instead..在secondViewController中创建属性FirstViewController *firstViewController
,再在secondViewController .m文件中对其进行合成...
Firstly do not create and allocate another instance of firstViewController in secondViewController..instead..create a property FirstViewController *firstViewController
in secondViewController further synthesize it in secondViewController .m file...
按照更正后的代码
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中
then in 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];
}
}
}
这篇关于将NSMutableArray从模态视图控制器传递到父视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!