如何为长按执行模态转场 [英] how to perform a modal segue for long press

查看:27
本文介绍了如何为长按执行模态转场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableview,我想当我触摸要导航到editViewController的单元格时,以及当我长触摸(触摸并等待)要导航到DetailsViewController的单元格时.我在这里得到了这个问题的答案.

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.

现在我有另一个问题,我使用以下代码将选定的行传递给 detailViewContoler

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
 }
}

现在我需要创建一个模态转场并在长按方法中将转场标识符设置为ContectDetails".

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

推荐答案

在表视图的视图控制器和 ContactDetailsViewController 之间的故事板中创建一个 segue,并在属性检查器中命名它(假设您将其命名为ContactDetailsModalSegue").

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').

然后在手势识别器处理程序方法中,您可以像这样调用 segue:

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

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

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

}

在这种情况下 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:

在表格视图单元格的 .h 文件中包含一个指向父 vc 的指针:

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)
}

最后在tableview单元格的.m文件中,使用指向vc的指针来调用segue:

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];

}

更新:为了将用于填充表格视图单元格的数据的引用传递给 destinationViewController,您可以执行以下操作:

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:

首先,确保数据存储在自定义表格视图单元格子类 .h 文件中的模型对象中.在这个例子中,我使用 NSManagedObject 是因为这就是你的问题,但其他人阅读这个可以用任何模型对象(例如 NSObject 子类)交换它:

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;

请注意,在 performSegueWithIdentifier: 方法调用中,您正在传递对 self 的引用作为发送者参数.您为 sender 参数指定的对象将作为参数传递给 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 方法.这使您可以检索刚刚添加到自定义表格视图单元格的公共属性,如下所示:

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;
        }
    }
}

这篇关于如何为长按执行模态转场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆