【问题标题】:Pass value with action sheet button使用操作表按钮传递值
【发布时间】:2016-10-20 12:14:33
【问题描述】:

我创建了一个带有按钮操作的ActionSheet。我有打开操作表的按钮,并且 ActionSheet 按钮打开新的viewController。问题是打开操作表的按钮在tableview cell 里面,我不知道如何用这个按钮传递值。

这个概念应该是这样的,但带有操作表按钮:

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

【问题讨论】:

  • 传值给什么? Action Sheet 还是 viewController?使用您的 actionSheet 代码块更新您的问题,并指定您想要该值的确切位置..

标签: swift firebase swift3 nsindexpath


【解决方案1】:

在按钮操作上,您正在显示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]
    }
}

【讨论】:

  • 单元格内的按钮是否像 selectedIndexPath 一样管理,也像单元格本身一样?但是像这样 selectedPost 传递的值是 nil :(
  • @TarvoMäesepp 不明白你想说什么,请详细解释。
  • 欢迎朋友 :)
【解决方案2】:

尝试使用委托方法。

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

     }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2012-07-19
    • 2017-02-21
    • 2011-04-12
    • 2014-05-08
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多