2013-11-29 118 views
-1

我已阅读了许多帖子,但似乎无法让我的数组在另一个类中可用。无法从另一个类访问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

使你的属性'强',并尝试.. –

+0

我只是试过,但计数仍然返回0.它可能必须做与重新加载tableview(所以numberofRowsInSection函数被调用两次)? – cdamayor

+0

看来你正在分配secondView然后把它扔掉。我猜你正在另一个地方创建另一个副本,这就是你初始化数组的地方,但这是一个完全不同的对象。 –

回答

0

我假设你想要的是获取存储在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初始化语句,请在这里使用secondView.plustransactions

希望这会有所帮助!

+0

好的。如果那条线的alloc init是这样的? CPPSecondViewController * secondView = secondView.plustransactions; Xcode是说,secondView将被初始化... – cdamayor

+0

你已经有secondView作为类的属性,只是在cellForRowAtIndexPath使用。例如:NSLog(@“%@%lu”,@“PlusTransactions”,(unsigned long)secondView.plusTransactions.count); – HRM

+0

这样做!谢谢你,结束了漫长的斗争:P – cdamayor

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

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

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

如果您想从历史视图控制器中引用第二个视图控制器,您可以在历史视图控制器中添加一个属性,请参阅第二个视图控制器。

在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]; 
+0

谢谢,但我如何触发属性初始化?我对Objective-C很陌生。 – cdamayor

+0

@cdamayor使用其他解决方案。我想你想参考历史视图中的第二个视图,所以刚过第二个视图到历史视图。 –

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

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

在历史课

,你NEDD创建它们,你可以在第二类中设置值的第二类的属性

@property (nonatomic, retain) Secondclass* secondclass; 

[secondClass setAry: ary];