具有自定义对象的全局数组不保持对象的状态

时间:2016-07-13 15:01:53

标签: ios objective-c uitextfield

我正在创建一个存储桶列表应用,我需要能够从BucketListGoal中的UITextField中保存每个UITableViewCell。以下是我的相关代码 -

@interface BucketListGoalViewController () <UITableViewDataSource, UITableViewDelegate, CreateGoalTableViewCellDelegate, UITextFieldDelegate, UIPopoverPresentationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *goals;
@property NSInteger path;
@end



@implementation BucketListGoalViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"Goals";
self.count = 0;

self.goals = [[NSArray alloc] init];
[self createNewGoal];

self.tableView.allowsSelection = NO;;
[self.tableView registerClass:[CreateGoalTableViewCell class]forCellReuseIdentifier:@"Cell"];
[self.tableView registerNib:[UINib nibWithNibName:@"CreateGoalTableViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleCoordinatesFromBucketList:)
                                             name:@"Coordinates"
                                           object:nil];


UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];



}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *cellIdentifier = @"Cell";

CreateGoalTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

[cell.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

cell.tag = indexPath.row;
cell.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textField.tag = indexPath.row;
cell.textField.delegate = self;
BucketListGoal *goal = [self.goals objectAtIndex:indexPath.row];
goal.tag = indexPath.row;

if (goal.address != nil) {
    [cell.locationButton setTitle:goal.address forState:UIControlStateNormal];

}
goal.name = cell.detailTextLabel.text;

cell.textField.placeholder = @"Write the description of your goal here.";

cell.numberLabel.text = [NSString stringWithFormat:@"%ld)", (long)indexPath.row + 1];
return cell;
}

-(void)createNewGoal {

BucketListGoal *goal = [BucketListGoal new];
NSMutableArray *copy = [self.goals mutableCopy];

[copy addObject:goal];


self.goals = copy;

[self.tableView reloadData];


}


-(void)textFieldDidChange:(UITextField *)textField{

NSMutableArray *copy = [self.goals mutableCopy];

BucketListGoal *goal = [self.goals objectAtIndex:textField.tag];


goal.name = textField.text;

[copy replaceObjectAtIndex:textField.tag withObject:goal];

self.goals = copy;

}

我从视图顶部的条形按钮项创建目标。问题是self.goals仅保留数组最新项的goal.name。文本字段中的所有其他先前项目继续为nil。我在这里缺少什么?

0 个答案:

没有答案