我是目标 c 的新手,但仍在试图找出整个面向目标的想法。在我的应用程序中,我有一个用于所有页面的复选标记(喜欢)按钮,因此我不想在每个视图中都有复选标记方法,而是想将它们移动到自己的类并调用它们。我该怎么做?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
...
//create check mark button
UIButton *checkButton = [[UIButton alloc] initWithFrame:CGRectMake(270, 0, 50, 90)];
//user has already check marked post
if ([checkActivityArray containsObject:[object objectId]]){
[checkButton setTitleColor: [UIColor blueColor] forState:UIControlStateNormal];
[checkButton addTarget:self action:@selector(minusCheckMark:) forControlEvents:UIControlEventTouchUpInside];
}
//user has NOT already check marked post
else{
[checkButton setTitleColor: [UIColor grayColor] forState:UIControlStateNormal];
[checkButton addTarget:self action:@selector(addCheckMark:) forControlEvents:UIControlEventTouchUpInside];
}
[checkButton setTitle:@"CH" forState:UIControlStateNormal];
[checkButton setTag:indexPath.row];
[cell.contentView addSubview:checkButton];
...
return cell;
}
- (void)addCheckMark:(UIButton*)button{
[button setTitleColor: [UIColor blueColor] forState:UIControlStateNormal];
PFObject *place = [placesArray objectAtIndex:[button tag]];
PFObject *checkActivity = [PFObject objectWithClassName:@"activity"];
[checkActivity setObject:place forKey:@"place"];
[checkActivity setObject:@"checkMark" forKey:@"type"];
[checkActivity setObject:[PFUser currentUser] forKey:@"fromUser"];
[checkActivity setObject:[place objectForKey:@"user"] forKey:@"toUser"];
[checkActivity saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error){
NSLog(@"Check marked post");
//add post to checkActivity array
[checkActivityArray addObject:[place objectId]];
[checkActivityArray writeToFile:checkMarkArrayFileName atomically:YES];
//increase check mark count
PFObject *checkCount = [placesArray objectAtIndex:[button tag]];
[checkCount incrementKey:@"checkMarkCount"];
[checkCount saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
[self.tableView reloadRowsAtIndexPaths: [NSArray arrayWithObject:[NSIndexPath indexPathForRow: [button tag] inSection:0]] withRowAnimation:NO];
}];
}
else
NSLog(@"%@", error);
}];
}
- (void)minusCheckMark:(UIButton*)button{
[button setTitleColor: [UIColor grayColor] forState:UIControlStateNormal];
PFObject *place = [placesArray objectAtIndex:[button tag]];
PFQuery *queryCheckActivity = [PFQuery queryWithClassName:@"activity"];
[queryCheckActivity whereKey:@"place" equalTo:place];
[queryCheckActivity whereKey:@"type" equalTo:@"checkMark"];
[queryCheckActivity whereKey:@"fromUser" equalTo:[PFUser currentUser]];
[queryCheckActivity findObjectsInBackgroundWithBlock:^(NSArray *activities, NSError *error) {
if (!error) {
for (PFObject *activity in activities) {
[activity delete];
}
//pemove post from checkActivity array
[checkActivityArray removeObject:[place objectId]];
[checkActivityArray writeToFile:checkMarkArrayFileName atomically:YES];
//decrease check mark count
PFObject *checkCount = [placesArray objectAtIndex:[button tag]];
[checkCount incrementKey:@"checkMarkCount" byAmount:[NSNumber numberWithInt:-1]];
[checkCount saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
[self.tableView reloadRowsAtIndexPaths: [NSArray arrayWithObject:[NSIndexPath indexPathForRow: [button tag] inSection:0]] withRowAnimation:NO];
}];
}
else
NSLog(@"%@", error);
}];
}