0

i have a tableview and i want to when I touch on the cells to be navigate to the editViewController and when I Long touch (touch and wait) on the cells to be navigate to the DetailsViewController. I got the answer to this question here.


now i have another problem, i use following code to pass selected row to detailViewContoler

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

if([[segue identifier] isEqualToString:@"ContectDetails"])
      {
         //get selected contact
         NSManagedObject *selectedContact=[contacts objectAtIndex:[[self.tableView   indexPathForSelectedRow] row]];

 //pass selected contact to MyContactAppViewController for editing
 ContactDetailsViewController *destViewcontroller=segue.destinationViewController;
 destViewcontroller.contact=selectedContact;
//contact is my core data object
 }
}

now i need to creat a modal segue and set segue identifier to "ContectDetails" in long press method.

Community
  • 1
  • 1

2 Answers2

0

replace [self.navigationController pushViewController:detailView animated:YES]; with [self.navigationController presentModalViewController:detailView animated:YES completion:nil]; in the method

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{ 
}
Ravi
  • 2,441
  • 1
  • 11
  • 30
  • This is not correct, `presentModalViewController` has been deprecated since iOS 6. [see this answer](http://stackoverflow.com/questions/12445190/dismissmodalviewcontrolleranimated-deprecated) – BFar Aug 28 '14 at 06:57
0

Create a segue in storyboard between your table view's view controller and the ContactDetailsViewController and name it in the attributes inspector (let's say you name it 'ContactDetailsModalSegue').

Then in the gesture recognizer handler method you can call the segue like this:

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { 

    [self performSegueWithIdentifier:@"ContactDetailsModalSegue"
                              sender:self];

}

In this case self should be the view controller that your table view resides in. If you are handling the long press inside the table view cell class, then you should keep a weak reference to the table view's view controller and refactor accordingly:

In your table view cell's .h file include a pointer to the parent vc:

@property (weak, nonatomic) UIViewController *vc;

Make sure you pass a reference to the cell when you set it up in the table view delegate:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    cell.vc = self;  //Assuming your datasource is in the view controller file (adjust if necessary)
}

and finally in the tableview cell's .m file, use the pointer to the vc to call the segue:

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { 

    [self.vc performSegueWithIdentifier:@"ContactDetailsModalSegue"
                                 sender:self];

}

Update: In order to pass a reference to the data that was used to populate the table view cell to the destinationViewController, you can do the following:

First, ensure that the data is stored in a model object in your custom table view cell subclass .h file. For this example I am using an NSManagedObject because thats what is in your question, but others reading this can swap this out with any model object (e.g. an NSObject subclass):

@property (strong, nonatomic) NSManagedObject *managedObject;

Note that in the performSegueWithIdentifier: method call you are passing a reference to self as the sender parameter. The object you specify for the sender parameter will be passed as an argument to the - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method. This enables you to retrieve the public property you just added to your custom table view cell, like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    //Verify segue identifier
    if([[segue identifier] isEqualToString:@"ContactDetailsModalSegue"])
    {
        //Protect yourself from unrecognized selector errors in case you reuse this segue
        if ([sender isKindOfClass:[YourCustomTableCellClass class]] &&
            [segue.destinationViewController respondsToSelector:@selector(setContact:)]) {

            //get selected contact
            NSManagedObject *selectedContact= (YourCustomTableCellClass *)sender.managedObject;


            //pass selected contact to ContactDetailsViewController for editing
            ContactDetailsViewController *destViewController= segue.destinationViewController;
            destViewController.contact = selectedContact;
        }
    }
}
BFar
  • 2,447
  • 22
  • 23
  • its work thanks, but in prepareForSegue method i need to pass selected cell to detailviewcontroller and when i long press on cell, it is not selected cell. Is a solution to this problem? – Mostafa Taghipour Aug 28 '14 at 17:27
  • thanks BFar but I could not find the answer. please check [this post](http://stackoverflow.com/questions/25556815/change-selection-of-cell-on-long-press-uitableview-ios) – Mostafa Taghipour Aug 28 '14 at 19:46