swift - 通过向左滑动操作点击时多个 ViewController

标签 swift uiviewcontroller uiswipegesturerecognizer

现在,我正在尝试通过向左滑动并点击之后的各种操作(编辑、添加、下载、删除)来连接不同的 ViewController。

如果只有 1 个操作(现在我已连接到编辑),我可以使用辅助操作来完成此操作。我尝试连接到 AddViewController 但不知何故它不再适用于附件操作。我知道只能有 1 个辅助操作,那么我应该如何开始解决这个问题?

最佳答案

我不太确定我是否正确理解了你的意思。您应该能够通过使用 UITableViewRowActionhandler block 来完成此操作,就像您对按钮 edit 所做的那样。我已经使用您的代码创建了几个示例,我希望这就是您的意思:

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject] {
    var shareAction = UITableViewRowAction(style: .Default, title: "Share") { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        if let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {

            if self.programmatically {
                // Segue to "shareFriendsSegue through a segue
                self.performSegueWithIdentifier("shareFriendsSegue", sender:self)
            } else {
                // Programmatically without identifier
                let destination1 = SomeViewController()
                destination1.dataYouWantToPass = self.someData
                self.navigationController?.pushViewController(destination1, animated: true)

                // Programmatically with an identifier
                let destination2 = self.storyboard?.instantiateViewControllerWithIdentifier("someViewController") as! SomeViewController
                destination2.dataYouWantToPass = self.someData
                self.showDetailViewController(destination2, sender: self)
            }
        }
    }

    var editAction = UITableViewRowAction(style: .Default, title: "Edit") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
        if let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
            let testObject = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSObject

            // Segue to "editSegue" through a segue
            self.performSegueWithIdentifier("editSegue", sender:self)
        }
    }

    var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in

        // Delete the row from tableView
        self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Right)

        // Delete the row from Core Data
        if let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
            let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
            context.deleteObject(object)
        }

        var error: NSError?
        if(self.managedObjectContext!.save(&error)) {
            if error != nil {
                println(error?.localizedDescription)
            }
        }
    }

    var fourth = UITableViewRowAction(style: .Default, title: "fourth") { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        if let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {

            if self.programmatically {
                // Segue to "shareFriendsSegue through a segue
                self.performSegueWithIdentifier("anotherSegue", sender:self)
            } else {
                // Programmatically without identifier
                let destination1 = AnotherViewController()
                destination1.dataYouWantToPass = self.someData
                self.navigationController?.pushViewController(destination1, animated: true)

                // Programmatically with an identifier
                let destination2 = self.storyboard?.instantiateViewControllerWithIdentifier("anotherViewController") as! AnotherViewController
                destination2.dataYouWantToPass = self.someData
                self.showDetailViewController(destination2, sender: self)
            }
        }

    }

    shareAction.backgroundColor = UIColor.greenColor()
    editAction.backgroundColor = UIColor(red: 255.0/255.0, green: 107.0/255.0, blue: 107.0/255.0, alpha: 1.0)
    deleteAction.backgroundColor = UIColor(red: 85.0/255.0, green: 98.0/255.0, blue: 112.0/255.0, alpha: 1.0)

    return [deleteAction, editAction, shareAction, fourth]
}

在这些示例中,使用 3 种不同的方式来更改 View :

  1. 通过一个segue。您可以在 prepareForSegue 方法中传递任何您想要的数据。
  2. 通过 Storyboard 进行实例化。您需要设置一个标识符,类似于设置 Segue 名称的方式。
  3. 使用初始值设定项创建 ViewController 的新实例。

请告诉我这是否是您正在寻找的内容。

关于swift - 通过向左滑动操作点击时多个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29901273/

相关文章:

ios - 检测字符串的语言

ios - 如何调用函数从 Swift 中的 UITableViewCell 类更改 viewController?

ios - 我应该如何在 RxSwift Singles 中正确使用 guard 语句?

ios - 如何在 iOS 应用程序中显示密码 View

ios - 如何从 TabBar Controller 禁用滑动手势识别器

ios - Swift - 执行从右到左的seguewithidentifier过渡样式

swift - 从 Parse.com 获取数据到 swift

iOS Swift popToViewController 按名称

iphone - 从 UIViewController 子类化时如何访问 UITableViewController 属性?

iOS创建滑动返回