我是目标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);
}];
}
答案 0 :(得分:0)
创建一个UIButon
类,将其添加到您需要添加的视图中,&和其他解决方案是添加按钮window
或navigation controler
。