我有一个appview,它是tableview / pickerview控制的,每个表都填充了textcellobjects。
现在,当客户选择一行并继续下一个tableview时,我想将该对象保存在一个类中分配/初始化的NSMutableArray中,并且我希望将该类文件设置为public,以便可以从所有不同的tableview添加对象类进入NSMutableArray。
前两个文件MotorTVController代码运行良好,当点击一个tableview单元格时,该对象被添加到_myTrimArray,但我希望能够在同一个_myTrimArray中保存下一个tablecell对象,我尝试导入TrimArray.h (这里的代码中有2个文件)与MotorTVController具有相同的保存功能,可以使用savearraycode,但是不起作用。
所以我希望TrimArray.h / TrimArray.h能够将tableviews和tableviewcell文本值从所有不同的类保存到一个NSMutableArray中。
我一直在努力解决这个问题差不多一个星期了,我希望有人可以给我一个解决方案,我对此有点新意,我知道这个问题很难解决,但我只是不知道&#39不知道怎么办?
//--------- MotorTVController.h
#import "YearPickerTVControllerPolaris.h"
#import "MotorTVController.h"
#import "motorTVCell.h"
#import "brandsTVCell.h"
#import "TrimArray.h"
@interface MotorTVController : YearPickerTVControllerPolaris
@property (nonatomic, strong) NSArray *motorLabelArray;
@property (nonatomic, strong) NSMutableArray *myTrimArray;
@end
//--------- MotorTVController.m file:
#import "MotorTVController.h"
#import "motorTVCell.h"
#import "YearPickerTVControllerPolaris.h"
#import "BackButton.h"
#import "TrimArray.h"
@interface MotorTVController ()
@end
@implementation MotorTVController
@synthesize myTrimArray = _myTrimArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
// Getter
- (NSMutableArray *)myTrimArray
{
if (_myTrimArray == nil) {
_myTrimArray = [[NSMutableArray alloc] init];
}
return _myTrimArray;
}
- (void)viewDidLoad
{
UIImage *arrow = [UIImage imageNamed:@"backbutton"];
UIButton *arrowButton = [[UIButton alloc] initWithFrame:CGRectMake(-12, 0, 36, 36)];
[arrowButton setBackgroundImage:arrow forState:UIControlStateNormal];
[arrowButton addTarget:self action:@selector(back)
forControlEvents:UIControlEventTouchUpInside];
UIView* buttonView = [[UIView alloc] initWithFrame:CGRectMake(-12, 0, 36, 36)];
[buttonView addSubview:arrowButton];
UIBarButtonItem * backbutton = [[UIBarButtonItem alloc]
initWithCustomView:buttonView];
[self.navigationItem setLeftBarButtonItem:backbutton];
[super viewDidLoad];
_motorLabelArray = @[@"Original EH12 (4hk)",
@"Honda GX160 (5.5hk)",
@"Subaru EX17 (6hk)",
@"Honda GX200 (6.5hk)",
@"Briggs LO206 (9hk)",
@"Briggs WF206 (11HK)"];
//NSLog(@"%@", _myTrimArray);
}
-(void)back
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _motorLabelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"motorTVCell";
motorTVCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
// Configure the cell...
[cell setTintColor:[UIColor blackColor]];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
[cell setAccessoryType:UITableViewCellAccessoryDetailButton];
NSInteger row = [indexPath row];
cell.motorCellLabel.text = _motorLabelArray[row];
return cell;
}
// Which row is selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
NSInteger rowNumber = indexPath.row;
if (rowNumber == 0)
{
[_myTrimArray addObject:[_motorLabelArray objectAtIndex:0]];
NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:0]);
}
else if (rowNumber == 1)
{
[_myTrimArray addObject:[_motorLabelArray objectAtIndex:1]];
NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:1]);
}
else if (rowNumber == 2)
{
[_myTrimArray addObject:[_motorLabelArray objectAtIndex:2]];
NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:2]);
}
else if (rowNumber == 3)
{
[_myTrimArray addObject:[_motorLabelArray objectAtIndex:3]];
NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:3]);
}
else if (rowNumber == 4)
{
[_myTrimArray addObject:[_motorLabelArray objectAtIndex:4]];
NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:4]);
}
else if (rowNumber == 5)
{
[_myTrimArray addObject:[_motorLabelArray objectAtIndex:5]];
NSLog(@"You choosed %@", [_myTrimArray objectAtIndex:5]);
}
}
@end
//-------------- TrimArray.m
#import "TrimArray.h"
#import "MotorTVController.h"
@interface TrimArray ()
@end
@implementation TrimArray
@synthesize myTrimArray = _myTrimArray;
// Getter
- (NSMutableArray *)myTrimArray
{
if (_myTrimArray == nil) {
_myTrimArray = [[NSMutableArray alloc] initWithCapacity:10];
}
return _myTrimArray;
}
@end
//----------------- TrimArray.h:
#import "MotorTVController.h"
@interface TrimArray : NSMutableArray
@property (nonatomic, strong) NSMutableArray *myTrimArray;
@end
答案 0 :(得分:0)
如果我理解你的问题,你想让一个arrayObject全局可用于所有其他ViewControllers吗?
如果是这样,你的子类化形式NSMutableArray并赋予它NSMutableArray的属性是错误的。
要实现这一点,最简单的解决方案是使用NSUserDefaults。
像下面一样疼痛。 //获取standardUserDefaults对象,针对密钥存储UITableView数据数组,同步默认值
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:_trimArray forKey:@"TrimArray"];
[userDefaults synchronize];
要检索 //您可以从应用中的任何位置检索arrayObject。
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayOfObjects = [userDefaults objectForKey:@"TrimArray"];
// Use 'yourArray' to repopulate your UITableView