0

我陷入了这个问题。我有一个带有几个视图控制器的故事板。

我需要做的是:我需要将一个数组从FirstViewControllerSecondViewController(它们不是邻居,也不是通过 segue 连接)传递给 PikerView 将上传数组的位置。之后,应将选取的结果传递给ThirdViewController. 我有选项卡式应用程序,其中FirstViewControllerSecondViewControllers连接到选项卡栏视图控制器并通过 Push SegueThirdViewController连接。SecondViewController

看看我如何尝试将数据形式从 First 传递到 Second

CategoryExpencesViewController.h

#import <UIKit/UIKit.h>
#import "AddCategoryViewController.h"
#import "CategoryPickerViewController.h"
@interface CategoryExpencesViewController : UITableViewController         <AddCategoryViewControllerDelegate>
@property(nonatomic, weak) IBOutlet UIBarButtonItem *editButton;
@property(nonatomic, strong) NSMutableArray *categories; //list of category items
@property(nonatomic, strong) NSMutableArray *listOfCategories; //list of category names

CategoryExpencesViewController.m

-(void)updateArray
{
CategoryPickerViewController *controller = [[CategoryPickerViewController alloc]init];
controller.categoryList = [[NSMutableArray alloc]init];
controller.categoryList = listOfCategories;
NSLog(@"%d", [listOfCategories count]);
NSLog(@"%d", [controller.categoryList count]);

}
4

3 回答 3

0

我想你会解决它,但我发布这个只是为了记录:

将数组包装成一个类,并使其具有静态构造方法:

包装器.h:

    @property (nonatomic, strong) NSMutableArray* array;
    +(Wrapper*)createArray;

包装器.m:

    +(Wrapper*)createArray{
        static Wrapper* instance = nil;
        if (instance == nil) {
            instance = [[Wrapper alloc] init];
            //Your initialization code for the array
        }
        return instance;
    }

然后,在您的 FirstViewController 中:

    -(void)updateArray{
        CategoryPickerViewController *controller = [[CategoryPickerViewController alloc]init];
        controller.categoryList = [[NSMutableArray alloc]init];
        controller.categoryList = [[Wrapper createArray] array];
        NSLog(@"%d", [listOfCategories count]);
        NSLog(@"%d", [controller.categoryList count]);

    }

由于这是对 Wrapper 的第一次调用,因此会生成数组。然后在您的 SecondViewController 中,当您调用时:

    secondView.categoryList = [[Wrapper createArray] array];

并且您获得与 FirstViewcontroller 中相同的数组。

于 2013-10-10T16:29:17.417 回答
0

我想你需要这个

使用你的 Push Segue。

segue.sourceViewController(或自我)将指向SecondViewControllersegue.sourceViewController.tabBarController将指向标签栏控制器。

从标签栏控制器你会发现你的FirstViewController.

于 2013-09-03T12:38:07.610 回答
-2

您是否想过 NSUserDefault 尝试一下,您还可以在 AppDelegate 类中创建一个实例全局变量,并通过在任何其他 ViewController 中创建 AppDelegate 的实例来访问它,但我认为 NSUserDefault 是最好的选择。

于 2013-09-03T12:43:29.233 回答