我已阅读了许多帖子,但似乎无法让我的数组在另一个类中可用。无法从另一个类访问NSMutableArray
我想从CPPSecondViewController类的CPPHistoryViewController(一个表格控制器,作为CPPSecondViewController的一个容器中的一个容器)访问数组的plusTransactions。
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.任何想法?
使你的属性'强',并尝试.. –
我只是试过,但计数仍然返回0.它可能必须做与重新加载tableview(所以numberofRowsInSection函数被调用两次)? – cdamayor
看来你正在分配secondView然后把它扔掉。我猜你正在另一个地方创建另一个副本,这就是你初始化数组的地方,但这是一个完全不同的对象。 –