gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:16:01 24 4
gpt4 key购买 nike

现在,我正在尝试通过向左滑动并点击之后的各种操作(编辑、添加、下载、删除)来连接不同的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com