0

I have a UITableViewRowAction so when I slide there are 3 options from which I can choose. If I click on the Call button I want a new ViewController to pop over the whole screen. And if I click on a button inside the new ViewController I want it to be dismissed.

On click on the Call button opens a ViewController as popover My UITableViewRowAction

This opens after my click on the button and this should dismiss on click on the bottom x-button My ViewController as popover

This is my code

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

I know that the code is I think very chaotic but I'm not yet very good at programming apps.

I appreciate any help and thank you in advance for your efforts.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
devtimg
  • 19
  • 8
  • Please describe your query in a little more detail... Uploading images and specifing your need would help better for us to guide you on the point. – Vedant Mahant Oct 30 '16 at 07:43
  • Above is my current code and I have a view controller with popOverPresentation which looks similar to the one on the picture. But I want to add a button to the view controller so it disappears when I click on the button to make the view controller disappear and my parent view controller shows up again. I upload two more pictures to give a better insight what I mean – devtimg Oct 30 '16 at 07:59
  • Why are you using simple `UIViewController`? Create a new `ViewController` in `xib/storyboard` instead, create an `@IBAction` and wire it to the dismiss button. – Adeel Miraj Oct 30 '16 at 08:07
  • I don't understand how I can connect the ViewController in storyboard so the UITableViewRowAction opens it first. – devtimg Oct 30 '16 at 08:10
  • you are actually confusing things. What you are trying to do is very easy and straightforward. I'll post an answer shortly that'll explain the matter. – Adeel Miraj Oct 30 '16 at 08:18
  • @devtimg have a look at my answer below. Hope it'll resolve your issue. – Adeel Miraj Oct 30 '16 at 09:03
  • code uploaded..let me know. if you need anything about my answer.... – Joe Oct 30 '16 at 11:00

2 Answers2

1

Do this instead of what you are doing:

  1. Create the ViewController that you want to present when call action is selected
  2. Place the dismiss button on that ViewController and wire it to an IBAction
  3. When user selects call action, instantiate that ViewController from the storyboard and simply present it

Here's a simple example:

Say this is the ViewController that you want to present when call action is triggered View Controller to be presented


Instantiate and present the ViewController in the 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
}


Just wire up your button with the 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)
    }
}
Adeel Miraj
  • 2,472
  • 3
  • 22
  • 32
  • Thank you very much for your answer. It works perfectly smooth. – devtimg Oct 30 '16 at 20:37
  • I upvoted your answer but it says: Votes cast by those with less than 15 reputations are recorded but do not change the publicly displayed post score. So I think I just have not enough reputation to do so. Sorry for that. – devtimg Oct 30 '16 at 21:30
  • Haha no issues it's absolutely fine – Adeel Miraj Oct 30 '16 at 21:42
0

Try this code: Tested in 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)
}
Joe
  • 8,868
  • 8
  • 37
  • 59
  • Thank you for your help but the answer above already worked for me. But I am thankful for your efforts – devtimg Oct 30 '16 at 20:37