首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从第二个视图控制器ios向我的基本视图控制器中的NSMutableArray添加对象

从第二个视图控制器ios向我的基本视图控制器中的NSMutableArray添加对象
EN

Stack Overflow用户
提问于 2013-06-22 10:17:27
回答 5查看 2.8K关注 0票数 2

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

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

然而,我不能做的是跨视图控制器获取信息。我整个上午都在研究如何使用应用程序代理、单例?、协议、共享属性等!但我还是被卡住了。

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-06-22 12:04:09

尝试使用委托方法,如下所示

Download Sample Project with XIBs

Download Sample Project With Storyboard

ParentViewController.h

代码语言:javascript
复制
#import <UIKit/UIKit.h>

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

ParentViewController.m

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
@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,那么在我的sample it showChildView中创建一个ParentViewController segue ChildViewController并给它一个identifier

然后使用以下代码设置委托

代码语言:javascript
复制
// 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;
    }
}

然后使用以下代码(取自我的示例)返回到ParentViewController

代码语言:javascript
复制
- (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];
}
票数 2
EN

Stack Overflow用户

发布于 2013-06-22 10:40:27

我建议使用您的NSMutableDictionary的单例实例,因为他们已经多次帮助我脱离了您的实际情况(包括自定义框架和UITabBarControllers)。这是我目前用来实现单例的一个例子。这种方法也是ARC-safe的

mySingleton.h

代码语言:javascript
复制
#import <Foundation/Foundation.h>

@interface mySingleton : NSObject {

}
+ (NSMutableDictionary *) myMutableDict;

@end

mySingleton.m

代码语言:javascript
复制
#import "mySingleton.h"

@implementation mySingleton

+ (NSMutableDictionary *)myMutableDict
{
    static NSMutableDictionary *singletonInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singletonInstance = [[NSMutableDictionary alloc]init];

    });
    return singletonInstance;
}

@end

只要在所有视图控制器中包含mySingleton.h,就可以通过[mySingleton myMutableDict]访问数据。例如:[[mySingleton myMutableDict] setObject:myObject forKey:myKey];

祝好运!

票数 1
EN

Stack Overflow用户

发布于 2013-06-22 11:11:59

如果信息真的是“全局的”--它在整个应用中只有一个实例--那么你应该按照DB80Buckeye的建议创建一个单例。

如果信息是真正属于ViewController1的东西,并且您希望在ViewController2中修改它(即ViewController2实际上是ViewController1的一部分,只是它恰好在另一个屏幕上),那么您应该将其作为ViewController2的构造函数的一部分进行传递。

代码语言:javascript
复制
-(void)view_controller_1_that_push_view_controller_2_onto_the_stack {
     ViewController2* vc2 = [[ViewController2 alloc] initWithInformation:your_information];
     [self.navigationController pushViewController:vc2 animated:YES];
}

@interface ViewController2 

-(id)initWithInformation:(YourInformationClass*)info;

@end

另一种方法是使用通知。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17246700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档