无法从另一个类访问NSMutableArray

时间:2013-11-29 03:52:29

标签: ios objective-c xcode

我在这里阅读了很多帖子,但我似乎无法在另一个课程中提供我的数组。

我想从CPPSecondViewController类访问CPPHistoryViewController中的数组plusTransactions(一个表控制器,在容器中作为CPPSecondViewController的子代)。

CPPSecondViewController.h

#import <UIKit/UIKit.h>
@interface CPPSecondViewController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedController;
@property (weak, nonatomic) NSMutableArray *plustransactions;
@property (weak, nonatomic) NSMutableArray *campustransactions;
@property (weak, nonatomic) NSMutableArray *mealtransactions;

@end

CPPSecondViewController.m

#import "CPPSecondViewController.h"
@interface CPPSecondViewController ()
@implementation CPPSecondViewController
@end

@synthesize plustransactions = _plustransactions;
@synthesize campustransactions = _campustransactions;
@synthesize mealtransactions = _mealtransactions;

...

_plustransactions = newPlustransactions;
NSLog(@"%@", newPlustransactions);
NSLog(@"%@%lu", @"That's how many: ", (unsigned long)_plustransactions.count);
CPPHistoryViewController *historyView = [[CPPHistoryViewController alloc]init];
[historyView.tableView reloadData];

这很好,返回我的所有数组项,计数为7。

CPPHistoryViewController.h

#import <UIKit/UIKit.h>

@interface CPPHistoryViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
@end

CPPHistoryViewController.m

#import "CPPHistoryViewController.h"
#import "CPPSecondViewController.h"
#import "CPPPlusTransaction.h"
#import "CPPCampusTransaction.h"
#import "CPPMealTransaction.h"
#import "CPPHistoryCell.h"

@interface CPPHistoryViewController ()

@end

@implementation CPPHistoryViewController

...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    CPPSecondViewController *secondView = [[CPPSecondViewController alloc] init];
    NSMutableArray *plusTransactions = [[NSMutableArray alloc]init];
    plusTransactions = secondView.plustransactions;
    NSMutableArray *campusTransactions = [[NSMutableArray alloc]init];
    campusTransactions = secondView.campustransactions;
    NSMutableArray *mealTransactions = [[NSMutableArray alloc]init];
    mealTransactions = secondView.mealtransactions;
    NSLog(@"Table formatting called");
    NSLog(@"%ld", (long)secondView.segmentedController.selectedSegmentIndex);
    switch (secondView.segmentedController.selectedSegmentIndex) {
        case 0:
            NSLog(@"%@%lu", @"PlusTransactions", (unsigned long)plusTransactions.count);
            return plusTransactions.count;
            break;
        case 1:
            NSLog(@"%@%lu", @"CampusTransactions", (unsigned long)campusTransactions.count);
            return campusTransactions.count;
            break;
        case 2:
            NSLog(@"%@%lu", @"MealTransactions", (unsigned long)mealTransactions.count);
            return mealTransactions.count;
            break;
    }
    return 1;
}

...

@end

这是事情变得奇怪的地方。我从这里调用的任何数组返回的计数为0.任何想法?

3 个答案:

答案 0 :(得分:1)

 CPPSecondViewController *secondView = [[CPPSecondViewController alloc] init];

这里你要创建一个新对象,所以所有的值都是nill。 在这里,您只能将值设置为第二个视图控制器。

在您的History类中,您需要创建第二个类的属性,您可以将它们设置为第二个类

@property (nonatomic, retain) Secondclass* secondclass;

[secondClass setAry: ary];

答案 1 :(得分:1)

我假设你想要的是让CPPSecondViewController中存储的数组数据在CPPHistoryViewController中可用。但根据您发布的代码,

CPPHistoryViewController *historyView = [[CPPHistoryViewController alloc]init];
[historyView.tableView reloadData];

这只是创建新实例,并重新加载视图中的tableView。但是在cellForRowAtIndexPath中,您正在创建CPPSecondViewController的新实例,这是错误的。

您可以做的是,只需在重新加载tableView之前传递CPPSecondViewController实例。

在CPPHistoryViewController.h中,创建一个用于保存secondView的属性

@property (strong, nonatomic) CPPSecondViewController *secondView;

然后,在CPPSecondViewController中更改您的代码,如下所示:

CPPHistoryViewController *historyView = [[CPPHistoryViewController alloc]init];
historyView.secondView = self;
[historyView.tableView reloadData];

此外,从cellForRowAtIndexPath删除alloc init语句,只需在此使用secondView.plustransactions

希望这有帮助!

答案 2 :(得分:0)

CPPSecondViewController *secondView = [[CPPSecondViewController alloc] init];

在这段代码中,视图控制器只是init,属性都是nil,它是一个新的视图控制器实例。

在init方法中设置属性,或者访问第二个视图的view属性,以加载视图,以触发属性初始化。

如果要从历史视图控制器引用第二个视图控制器,可以在历史视图控制器中添加属性,参考第二个视图控制器。

在CPPHistoryViewController.h中

@property (weak, nonatomic) CPPSecondViewController *secondViewController;

并在展示代码中设置此属性。

在CPPSecondViewController.m

_plustransactions = newPlustransactions;
NSLog(@"%@", newPlustransactions);
NSLog(@"%@%lu", @"That's how many: ", (unsigned long)_plustransactions.count);
CPPHistoryViewController *historyView = [[CPPHistoryViewController alloc]init];
historyView.secondViewController = self;
[historyView.tableView reloadData];