gpt4 book ai didi

ios - 从第二个 View Controller ios 将对象添加到我的基本 View Controller 中的 NSMutableArray

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:17:51 26 4
gpt4 key购买 nike

我整个上午都在寻找如何做到这一点。我有 2 个 View Controllers。从根 View Controller(ViewControllerA - 这是一个 TableView Controller ),您可以推送到第二个 View Controller (ViewControllerB)。

ViewControllerB 中,有两个字段:contacts 和 textBody。用户完成后,他们可以单击“添加”。然后这将返回到 ViewControllerA。我现在要做的是,每次发生该过程时,用户刚刚添加的 ViewControllerB 中的所有信息都会进入 ViewControllerA 中的单元格。然后用户可以添加任意数量的单元格。

但是,我不能做的是跨 View Controller 获取信息。我整个上午都在研究如何使用 app delegate、singletons??、协议(protocol)、共享属性等!但我仍然卡住了。

我想做但做不到的是,每次用户在 ViewControllerB 上单击“添加”时,联系人和文本都会放入一个数组中。然后将该数组放入另一个数组中,该数组包含用户创建的所有较小数组?如果您有想法或类似/示例代码或教程的链接,我们将不胜感激!

最佳答案

使用如下委托(delegate)方法试试这个

Download Sample Project with XIBs

Download Sample Project With Storyboard

ParentViewController.h

#import <UIKit/UIKit.h>

@interface ParentViewController : UIViewController {
NSMutableArray *dataArray;
}
- (void)passData:(NSMutableArray *)array;
@end

ParentViewController.m

#import "ParentViewController.h"
#import "ChildViewController.h"

@implementation ParentViewController

- (void)viewDidLoad {
[super viewDidLoad];

// Initialise the mutable array.
dataArray = [[NSMutableArray alloc] init];
}

- (IBAction)btnGoToSecondView:(id)sender {
ChildViewController *secondVC = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
secondVC.delegate = self;
[self presentViewController:secondVC animated:YES completion:nil];
}

- (void)passData:(NSMutableArray *)array {
[dataArray addObject:array];
NSLog(@"Data Passed = %@",dataArray);
}
@end

ChildViewController.h

#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@class ParentViewController;

@interface ChildViewController : UIViewController {
NSMutableArray *tempArray;
}

@property (strong, nonatomic) IBOutlet UITextField *txtContact;
@property (strong, nonatomic) IBOutlet UITextField *txtTextBody;
@property(nonatomic, assign) ParentViewController *delegate;
@end

ChildViewController.m

@implementation ChildViewController

- (void)viewDidLoad {
[super viewDidLoad];

// Initialise the mutable array.
tempArray = [[NSMutableArray alloc] init];
}

- (IBAction)btnPassDataBack:(id)sender {
if([self.delegate respondsToSelector:@selector(passData:)]) {

[tempArray addObject:_txtContact.text];
[tempArray addObject:_txtTextBody.text];

[self.delegate passData:tempArray];
}
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidUnload {
[self setTxtContact:nil];
[self setTxtTextBody:nil];
[super viewDidUnload];
}
@end

带有 Storyboard

如果您正在使用 Storyboard,那么创建一个 ParentViewController segue ChildViewController 并在我的示例中为其提供一个 identifier showChildView

然后使用下面的代码设置delegate

// Calling the segue to go to the child view and setting up the delegate.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showChildView"]) {
ChildViewController *childVC = segue.destinationViewController;
childVC.delegate = self;
}
}

然后使用以下代码(来 self 的示例)返回 ParentViewController

- (IBAction)btnPassDataBack:(id)sender {
if([self.delegate respondsToSelector:@selector(passData:)]) {

[tempArray addObject:_txtContact.text];
[tempArray addObject:_txtTextBody.text];

[self.delegate passData:tempArray];
}
[self.navigationController popToRootViewControllerAnimated:YES];
}

关于ios - 从第二个 View Controller ios 将对象添加到我的基本 View Controller 中的 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17246700/

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