使用操作 sheet 按钮传递值
Pass value with action sheet button
我创建了一个带有按钮操作的 ActionSheet
。我有打开操作的按钮 sheet 和 ActionSheet 按钮打开新的 viewController
。问题是打开操作 sheet 的按钮在 tableview cell
里面,我不知道如何用这个按钮传递值。
概念应该是这样的,但是带有操作 sheet 按钮:
actionSheet.addAction(UIAlertAction(title: "Edit it", style: UIAlertActionStyle.destructive, handler: { (ACTION :UIAlertAction!)in
//here I want to pass value to next viewController and open that VC.
//Valu should be something like postsArray[indexPath.row]
}))
在按钮操作上显示 ActionSheet
并且您希望该按钮在 prepareforSegue
方法上的索引,因此您需要在按钮操作上存储点击按钮的 IndexPath
然后呈现 ActionSheet
声明类型 IndexPath
的一个实例并在您的按钮操作中使用它。
var selectedIndexPath = IndexPath()
@IBAction func buttonTapped(_ sender: UIButton) {
let point = tableView.convert(CGPoint.zero, from: sender)
if let indexPath = tableView.indexPathForRow(at: point) {
self.selectedIndexPath = indexPath
//Add code for presenting ActionSheet
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editPost" {
let dest = segue.destination as! EditPostViewController
dest.selectedPost = postsArray[self.selectedIndexPath.row]
}
}
尝试使用委托方法。
protocol ActionSheetDelegate{
func transferSomeData(message : String)
}
class customCell : UITableViewCell {
var customCellDelegate : ActionSheetDelegate!
...
actionSheet.addAction(UIAlertAction(title: "Edit it", style: UIAlertActionStyle.destructive, handler: { (ACTION :UIAlertAction!)in
self.customCellDelegate.transferSomeData(message : "yourMessage")
})
)
...
}
在你当前的tableViewController
class yourTableViewController : UITableViewController, UITableViewDelegate, UITableViewDataSource, ActionSheetDelegate {
override func viewDidLoad(){
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = customTableView.dequeueReusableCell(withIdentifier: "customCell") as! customTableViewCell
cell.customCellDelegate = self
return cell
}
func transferSomeData(message : String){
print(message)
// Segue to someViewController
}
}
我创建了一个带有按钮操作的 ActionSheet
。我有打开操作的按钮 sheet 和 ActionSheet 按钮打开新的 viewController
。问题是打开操作 sheet 的按钮在 tableview cell
里面,我不知道如何用这个按钮传递值。
概念应该是这样的,但是带有操作 sheet 按钮:
actionSheet.addAction(UIAlertAction(title: "Edit it", style: UIAlertActionStyle.destructive, handler: { (ACTION :UIAlertAction!)in
//here I want to pass value to next viewController and open that VC.
//Valu should be something like postsArray[indexPath.row]
}))
在按钮操作上显示 ActionSheet
并且您希望该按钮在 prepareforSegue
方法上的索引,因此您需要在按钮操作上存储点击按钮的 IndexPath
然后呈现 ActionSheet
声明类型 IndexPath
的一个实例并在您的按钮操作中使用它。
var selectedIndexPath = IndexPath()
@IBAction func buttonTapped(_ sender: UIButton) {
let point = tableView.convert(CGPoint.zero, from: sender)
if let indexPath = tableView.indexPathForRow(at: point) {
self.selectedIndexPath = indexPath
//Add code for presenting ActionSheet
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editPost" {
let dest = segue.destination as! EditPostViewController
dest.selectedPost = postsArray[self.selectedIndexPath.row]
}
}
尝试使用委托方法。
protocol ActionSheetDelegate{
func transferSomeData(message : String)
}
class customCell : UITableViewCell {
var customCellDelegate : ActionSheetDelegate!
...
actionSheet.addAction(UIAlertAction(title: "Edit it", style: UIAlertActionStyle.destructive, handler: { (ACTION :UIAlertAction!)in
self.customCellDelegate.transferSomeData(message : "yourMessage")
})
)
...
}
在你当前的tableViewController
class yourTableViewController : UITableViewController, UITableViewDelegate, UITableViewDataSource, ActionSheetDelegate {
override func viewDidLoad(){
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = customTableView.dequeueReusableCell(withIdentifier: "customCell") as! customTableViewCell
cell.customCellDelegate = self
return cell
}
func transferSomeData(message : String){
print(message)
// Segue to someViewController
}
}