gpt4 book ai didi

iphone - 将 NSMutableArray 从模态视图 Controller 传递到父 View

转载 作者:行者123 更新时间:2023-11-28 22:47:11 25 4
gpt4 key购买 nike

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

这是我目前的方法:

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

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

最佳答案

不要在 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;
}
}

关于iphone - 将 NSMutableArray 从模态视图 Controller 传递到父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12911115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com