我想通过UITableViewRowButtonAction打开一个新的ViewController

时间:2016-10-30 06:32:07

标签: ios swift uipopovercontroller uitableviewrowaction

我有一个UITableViewRowAction所以当我滑动时,我可以选择3个选项。如果我点击Call按钮,我想要一个新的ViewController来弹出整个屏幕。如果我点击新ViewController 内的按钮,我希望将其解除

单击“调用”按钮,将ViewController打开为弹出窗口 My UITableViewRowAction

点击按钮后会打开,点击底部的x按钮就会解除此问题 My ViewController as popover

这是我的代码

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

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

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

2 个答案:

答案 0 :(得分:0)

这样做而不是你在做什么:

  1. 创建选择SELECT c0.*, p1."id" FROM "c0" AS c0 LEFT JOIN "p1" AS p1 ON p1."id" = 2 LEFT JOIN "c2" AS c2 ON c2."p1" = p1."id" WHERE (c2."c1_id" = c0."id") GROUP BY c0."id", p1."id" 时要显示的ViewController
  2. call action按钮放在dismiss上并将其连接到ViewController
  3. 当用户选择IBAction时,从call action实例化ViewController并简单地展示
  4. 这是一个简单的例子:

    说这是触发呼叫操作时要显示的storyboard View Controller to be presented


    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

答案 1 :(得分:0)

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

<强> MainVC:

  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]

}

<强> DestVC

@IBAction func dissmissButton(_ sender: UIButton) {

    dismiss(animated: true, completion: nil)
}