0

我有一个UITableViewRowAction所以当我滑动时,我可以选择 3 个选项。如果我单击Call按钮,我希望ViewController在整个屏幕上弹出一个新按钮。如果我点击 new 里面的一个按钮,ViewController 我希望它被解雇

单击 Call 按钮会打开一个 ViewController 作为弹出窗口 我的 UITableViewRowAction

这会在我点击按钮后打开,点击底部的 x 按钮会关闭 我的 ViewController 作为弹出框

这是我的代码

func buttonToDismiss (sender: AnyObject) {

    self.presentedViewController?.dismiss(animated: true, completion: nil)
}


func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let callButton = UITableViewRowAction(style: .default, title: "Call", handler: { (action, indexPath) in
        self.tableView.dataSource?.tableView?(
            self.tableView,
            commit: .delete,
            forRowAt: indexPath)

        let vc = UIViewController(nibName: nil, bundle: nil)
        vc.view.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
        vc.view.backgroundColor = UIColor(red: 62/255.0, green: 70/255.0, blue: 80/255.0, alpha: 1.0)
        vc.modalPresentationStyle = .popover


        let declineButton = UIButton()
        declineButton.frame = CGRect(x: 150, y: 484, width: 75, height: 75)
        declineButton.backgroundColor = UIColor(red: 36/255.0, green: 44/255.0, blue: 55/255.0, alpha: 1.0)
        declineButton.tintColor = UIColor.white
        declineButton.layer.cornerRadius = declineButton.frame.size.height / 2
        declineButton.layer.masksToBounds = true
        declineButton.clipsToBounds = true
        declineButton.setTitle("X", for: .normal)
        declineButton.addTarget(self, action: Selector(("buttonToDismiss:")), for: UIControlEvents.touchUpInside)
        vc.view.addSubview(declineButton)
        let popover = vc.popoverPresentationController!
        let cell = tableView.cellForRow(at: indexPath)!

        var cellAbsolutePosition = cell.superview!.convert(cell.frame.origin, to: nil)
        cellAbsolutePosition.x = cell.frame.width - 60
        popover.sourceRect = CGRect(origin: cellAbsolutePosition, size: cell.frame.size)
        popover.sourceView = tableView

        self.present(vc, animated: true, completion: nil)

        return
    })

我知道我认为代码很混乱,但我还不太擅长编写应用程序。

感谢您的帮助,并提前感谢您的努力。

4

2 回答 2

1

这样做而不是你正在做的事情:

  1. 创建ViewController您要在call action选择时呈现的
  2. dismiss按钮放在上面ViewController并将其连接到IBAction
  3. 当用户选择时,从 中call action实例化它并简单地呈现它ViewControllerstoryboard

这是一个简单的例子:

假设这是ViewController您在触发呼叫操作时要呈现的内容 要呈现的视图控制器


ViewController实例化 并呈现call action

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    var rowActions = [UITableViewRowAction]()

    let callAction = UITableViewRowAction.init(style: .default,
                                               title: "Call") { (action, cellPath) in
                                                //instantiate the view controller with storyboard ID
                                                let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
                                                self.present(vc, animated: true, completion: { 

                                                })
    }

    rowActions.append(callAction)

    return rowActions
}


只需将您的按钮与IBAction

class DetailViewController: UIViewController {

    var delegate: DetailDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func didTapBtn(_ sender: AnyObject) {
        dismiss(animated: true, completion: nil)
    }
}
于 2016-10-30T09:02:16.020 回答
0

试试这个代码:在 Swift 3 中测试。

主VC:

  func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
  }

  func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
    return true
  }

  func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let callAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Call") { (UITableViewRowAction, NSIndexPath) -> Void in
        self.performSegue(withIdentifier: "callSegue", sender: self) 
    }

    let videoAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Video") { (UITableViewRowAction, NSIndexPath) -> Void in
    }

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Delete") { (UITableViewRowAction, NSIndexPath) -> Void in  
    }

    callAction.backgroundColor = .blue
    videoAction.backgroundColor = .green
    deleteAction.backgroundColor = .red

    return [deleteAction,videoAction,callAction]

}

目标VC

@IBAction func dissmissButton(_ sender: UIButton) {

    dismiss(animated: true, completion: nil)
}
于 2016-10-30T10:59:39.883 回答