Gino
Gino

Reputation: 951

Multiple ViewControllers upon tapping with swipe left action

Right now, I'm trying to connect different ViewControllers with various actions ( edit, add, download, delete ) upon swiping to the left and tapping thereafter.

I can do it with accessory actions, if it is just 1 action ( which for now I've hooked up to edit ). I've attempted to hooked up to AddViewController but somehow it won't work anymore with accessory action. I understand that there can only be 1 accessory action, so how should I even begin to resolve this?

Upvotes: 0

Views: 182

Answers (1)

Eendje
Eendje

Reputation: 8883

I'm not very sure if I understood you correctly. You should be able doing that just by using the handler block of UITableViewRowAction the way you have been doing for the button edit. I've used your code to create several examples, I hope this is what you meant:

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

In these examples 3 different ways are used to change the view:

  1. Through a segue. You can pass whatever data you want in the method prepareForSegue.
  2. Instantiating through the storyboard. You'll need to set an identifier, similar to how you set a segue name.
  3. Create a new instance of a ViewController using an initializer.

Let me know if this is what you were searching for.

Upvotes: 1

Related Questions