来自editActionsForRowAtIndexPath的Segue [英] Segue from editActionsForRowAtIndexPath

查看:81
本文介绍了来自editActionsForRowAtIndexPath的Segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于指导目的,我正在制作一个小型银行类应用程序.

I'm making a little banking type app, for instructional purposes.

所有事务都存储为Core Data实体,使用Magical Record助手方法进行获取和排序.事务显示在UITableView中,并由fetchedObjects数组提供.

All transactions are stored as Core Data entities, fetched and sorted using Magical Record helper methods. The transactions are displayed in a UITableView, fed by a fetchedObjects array.

我希望能够像这样使用editActionsForRowAtIndexPath:

I want to be able to use editActionsForRowAtIndexPath like so:

第一个动作锁定到另一个uiviewController,第二个和第三个动作锁定到UIPopoverPresentationController.第四个按钮删除交易.

The first action segues to another uiviewController, the second and third to a UIPopoverPresentationController. The fourth button deletes the transaction.

以下是创建按钮的代码:

Here's the code to create the buttons:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    //Obviously, if this returns no, the edit option won't even populate
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //Nothing gets called here if you invoke `tableView:editActionsForRowAtIndexPath:` according to Apple docs so just leave this method blank
}

-(NSArray *)myArray:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                    {
                                        // Delete code here
                                    }];

    UITableViewRowAction *changeAmt = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Amt" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                       {
                                           // popover amount change code
                                       }];

    changeAmt.backgroundColor = [UIColor colorWithRed:0.022 green:0.541 blue:0.001 alpha:1.00];


    UITableViewRowAction *changeAcct = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Acct" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                        {
                                            // popover account change code
                                        }];

    changeAcct.backgroundColor = [UIColor colorWithRed:1.000 green:0.492 blue:0.000 alpha:1.00];

    UITableViewRowAction *viewTrans = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"View" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                        {
                                            // Push to view
                                        }];
    viewTrans.backgroundColor = [UIColor colorWithRed:0.002 green:0.342 blue:0.710 alpha:1.00];


    return @[delete, changeAmt,changeAcct,viewTrans]; //array with the buttons delete, changeAmt, changeAcct, viewTrans
}

这是我的问题:

我如何知道哪个按钮触发哪个序列?我不知道如何建立连接.

How do I tell which button to trigger which segue? I can't figure how to make the connections.

这是我的prepareForSegue中具有代表性的代码块. ???表明了我的困惑.在sourceRect的参数中:

Here's a representative chunk of code from my prepareForSegue. One point of my confusion is indicated by the ??? in the argument for the sourceRect:

else if ([[segue identifier] isEqualToString:@"AmountPopSegue"])
{
    UIViewController *controller = segue.destinationViewController;
    controller.popoverPresentationController.delegate = self;
    controller.preferredContentSize = CGSizeMake(320, 186);
    UIPopoverPresentationController *thisPPC = controller.popoverPresentationController;


    thisNavController = (UINavigationController *)segue.destinationViewController;
    AmountChangePopVC *acVC = (AmountChangePopVC *)thisNavController.topViewController;
    acVC.delegate = self;
    thisPPC.sourceRect = ???;
}

感谢所有见解.我之所以写Objective C,是因为我在Swift风靡一时之前就开始了这个项目.

All insights appreciated. I'm writing in Objective C because I started this project before Swift became all the rage.

推荐答案

此方法可以为您提供帮助.您将知道轻按了哪个按钮.

This method can help you. You'll know which button was tapped.

-注意.此方法仅在iOS 8或更高版本中有效.-

--Be careful. this method only work in iOS 8 or later.--

@property (nonatomic, strong) NSIndexPath *firstButtonIndexPath;
@property (nonatomic, strong) NSIndexPath *secondButtonIndexPath;
@property (nonatomic, strong) NSIndexPath *thirdButtonIndexPath;
@property (nonatomic, strong) NSIndexPath *fourthButtonIndexPath;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        if (self.firstButtonIndexPath) {
            NSLog(@"ViewButton :%ld", (long)self.firstButtonIndexPath.row);
        } else if (self.secondButtonIndexPath) {
            NSLog(@"Acct :%ld", (long)self.secondButtonIndexPath.row);
        } else if (self.thirdButtonIndexPath) {
            NSLog(@"Amt :%ld", (long)self.thirdButtonIndexPath.row);
        } else if (self.fourthButtonIndexPath) {
            NSLog(@"Delete :%ld", (long)self.fourthButtonIndexPath.row);
        }
    }
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.firstButtonIndexPath = nil;
    self.secondButtonIndexPath = nil;
    self.thirdButtonIndexPath = nil;
    self.fourthButtonIndexPath = nil;

    UITableViewRowAction *viewAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"View" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"View the row");
        self.firstButtonIndexPath = indexPath;
    }];
    viewAction.backgroundColor = [UIColor blueColor];

    UITableViewRowAction *acctAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Acct" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"Acct the row");
        self.secondButtonIndexPath = indexPath;
    }];
    acctAction.backgroundColor = [UIColor orangeColor];

    UITableViewRowAction *amtAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Amt" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"Amt the row");
        self.thirdButtonIndexPath = indexPath;
    }];
    amtAction.backgroundColor = [UIColor greenColor];

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"Delete the row");
        self.fourthButtonIndexPath = indexPath;
    }];
    deleteAction.backgroundColor = [UIColor redColor];

    return @[deleteAction, amtAction, acctAction, viewAction];
}

这篇关于来自editActionsForRowAtIndexPath的Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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