I have a UITableview
with custom cells, every cell has a button called cellButton
. When the user taps the button I present an action sheet which has two buttons, buttonOne
and buttonTwo
. When buttonOne
has been tapped I call a method with some values from the actual cell, when buttonTwo
I perform a segue and prepareForSegue:
method to pass data from the table view cell to another VC. I have a working solution, but I'm not sure that this is the best way because I'm passing the PFUser
object from the index path to an instance variable and only use it when the user choose something in the action sheet. I never needed passing values like this, I always used directly the NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];..
way in prepareForSegue:
, therefore I'm a bit confused now. Is it a correct way or can it go wrong in any case?
@property (nonatomic, strong) PFUser *userObj;
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
PFUser *user = [self.dataSourceArray objectAtIndex:indexPath.row];
cell.usernm.text = user.username;
cell.userId.text = user.objectId;
cell.cellButton.tag = indexPath.row;
[cell.cellButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)buttonTapped:(id)sender {
UIButton *button = (UIButton *)sender;
self.userObj = [self.dataSourceArray objectAtIndex:button.tag];
[self setupActionSheet];
}
- (void) setupActionSheet {
AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithTitle:NSLocalizedString(nil, nil)];
[actionSheet addButtonWithTitle:NSLocalizedString(@"Button 1", nil)
image:[UIImage imageNamed:@"1"]
type:AHKActionSheetButtonTypeDefault
handler:^(AHKActionSheet *as) {
[Utilz checkUser:self.userObj.objectId with:self.userObj.username];
}];
[actionSheet addButtonWithTitle:NSLocalizedString(@"Button 2", nil)
image:[UIImage imageNamed:@"2"]
type:AHKActionSheetButtonTypeDefault
handler:^(AHKActionSheet *as) {
[self performSegueWithIdentifier:@"presentNextView" sender:self];
}];
[actionSheet show];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"presentNextView"]) {
if ([[segue destinationViewController] isKindOfClass:[ViewControllerTwo class]]) {
ViewControllerTwo *dataToPass = [segue destinationViewController];
dataToPass.usrStr = self.usrObj.username;
dataToPass.objStr = self.usrObj.objectId;
}
}
}