我整个上午都在寻找如何做到这一点。我有两个View Controllers。从根View Controller (ViewControllerA -这是一个表视图控制器),您可以推送到第二个视图控制器(ViewControllerB)。
在ViewControllerB中,有两个字段: contacts & textBody。当用户完成后,他们可以点击“添加”。然后,这将返回到ViewControllerA。我现在尝试做的是,每次这个过程发生时,用户刚添加的来自ViewControllerB的所有信息都会进入ViewControllerA中的一个单元格。然后,用户可以添加任意数量的单元格。
然而,我不能做的是跨视图控制器获取信息。我整个上午都在研究如何使用应用程序代理、单例?、协议、共享属性等!但我还是被卡住了。
我想做的,但不能做的是,每次用户在ViewControllerB上点击“添加”时,联系人和文本都会放入一个数组中。然后将此数组放入另一个数组中,该数组保存用户创建的所有较小的数组?如果你有一个想法,或链接到类似/示例代码或教程,将不胜感激!
发布于 2013-06-22 12:04:09
尝试使用委托方法,如下所示
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;
@endParentViewController.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);
}
@endChildViewController.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;
@endChildViewController.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,那么在我的sample it showChildView中创建一个ParentViewController segue ChildViewController并给它一个identifier
然后使用以下代码设置委托
// 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
- (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];
}发布于 2013-06-22 10:40:27
我建议使用您的NSMutableDictionary的单例实例,因为他们已经多次帮助我脱离了您的实际情况(包括自定义框架和UITabBarControllers)。这是我目前用来实现单例的一个例子。这种方法也是ARC-safe的
mySingleton.h
#import <Foundation/Foundation.h>
@interface mySingleton : NSObject {
}
+ (NSMutableDictionary *) myMutableDict;
@endmySingleton.m
#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];
祝好运!
发布于 2013-06-22 11:11:59
如果信息真的是“全局的”--它在整个应用中只有一个实例--那么你应该按照DB80Buckeye的建议创建一个单例。
如果信息是真正属于ViewController1的东西,并且您希望在ViewController2中修改它(即ViewController2实际上是ViewController1的一部分,只是它恰好在另一个屏幕上),那么您应该将其作为ViewController2的构造函数的一部分进行传递。
-(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另一种方法是使用通知。
https://stackoverflow.com/questions/17246700
复制相似问题