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

标签 ios objective-c uiviewcontroller segue sharing

我整个上午都在寻找如何做到这一点。我有 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/

相关文章:

iOS 架构 i386 的 undefined symbol

ios - iOS-在“NSArray”类型的对象上找不到属性“名称”

ios - UISplitViewController - 隐藏主滑入 View

ios - 使用 Social Framework 登录并列出 Facebook 好友

iphone - 警告 : Attempt to present <AdjustPhotoViewController> on <TakePhotoViewController> while a presentation is in progress

ios - 关闭多个 UIView 的正确方法?

ios - 如何将 NSAppTransportSecurity 添加到 Cordova 项目

iphone - 将二进制的 "String"转换为文本的 NSString

ios - 无法解密AES256密文

iphone - 使用 initWithNibName : bundle: or via an IBOutlet behave differently 创建的 UIViewController