通过向左滑动操作点击多个 ViewController
Multiple ViewControllers upon tapping with swipe left action
现在,我正在尝试通过向左滑动并点击之后的各种操作(编辑、添加、下载、删除)连接不同的 ViewController。
我可以用辅助动作来完成,如果它只是 1 个动作(现在我已经连接起来编辑)。我试图连接到 AddViewController 但不知何故它不再适用于辅助操作。我知道只能有 1 个辅助操作,那么我应该如何开始解决这个问题?
我不太确定我是否理解正确。你应该能够通过使用 UITableViewRowAction
的 handler
块来做到这一点,就像你为按钮 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 种不同的方式来更改视图:
- 通过segue。您可以在方法
prepareForSegue
. 中传递任何您想要的数据
- 通过故事板实例化。您需要设置一个标识符,类似于设置 segue 名称的方式。
- 使用初始化程序创建 ViewController 的新实例。
如果这就是您要搜索的内容,请告诉我。
现在,我正在尝试通过向左滑动并点击之后的各种操作(编辑、添加、下载、删除)连接不同的 ViewController。
我可以用辅助动作来完成,如果它只是 1 个动作(现在我已经连接起来编辑)。我试图连接到 AddViewController 但不知何故它不再适用于辅助操作。我知道只能有 1 个辅助操作,那么我应该如何开始解决这个问题?
我不太确定我是否理解正确。你应该能够通过使用 UITableViewRowAction
的 handler
块来做到这一点,就像你为按钮 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 种不同的方式来更改视图:
- 通过segue。您可以在方法
prepareForSegue
. 中传递任何您想要的数据
- 通过故事板实例化。您需要设置一个标识符,类似于设置 segue 名称的方式。
- 使用初始化程序创建 ViewController 的新实例。
如果这就是您要搜索的内容,请告诉我。