通过操作表按钮传递值 [英] Pass value with action sheet button

查看:133
本文介绍了通过操作表按钮传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这个概念应该是这样的,但是用动作表按钮:

pre $ action $ s $ action $ title:Edit it,style:UIAlertActionStyle.destructive,handler:{(ACTION:UIAlertAction!)in
//这里我想把值传递给下一个viewController并打开VC
// Valu应该像postsArray [indexPath.row]
)))


解决方案

在按钮操作上,你正在显示 ActionSheet ,你想在 prepareforSegue 方法上使用该按钮的索引,所以你需要在按钮的动作中存储点击按钮的 IndexPath ,然后显示 ActionSheet 类型的实例 IndexPath 并在哟内使用它urb按钮操作。

pre $ var $ selectedIndexPath = IndexPath()
$ b @IBAction func buttonTapped(_ sender: UIButton){
let point = tableView.convert(CGPoint.zero,from:sender)
if indexPath = tableView.indexPathForRow(at:point){
self.selectedIndexPath = indexPath
//添加代码来呈现ActionSheet
}
}

覆盖func prepare(for segue:UIStoryboardSegue,sender:Any?){
if segue.identifier ==editPost{
let dest = segue.destination as! EditPostViewController
dest.selectedPost = postsArray [self.selectedIndexPath.row]
}
}


I created an ActionSheet with action of button. I have button that opens the action sheet and the ActionSheet button opens new viewController. The problem is that the button which opens the action sheet is inside tableview cell and I do not know how to pass value with this button.

The concept should be something like this but with the action sheet button:

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

解决方案

On button action you are showing the ActionSheet and you want that button's index on prepareforSegue method, so you need to store the IndexPath of tapped button on the action of button and then present the ActionSheet for that declare one instance of type IndexPath and use it inside your button action.

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

这篇关于通过操作表按钮传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆