简体   繁体   中英

How to present a separate View Controller when click on a button inside a custom TableViewCell Swift 4?

I have 2 view controller, MainVC and PopUpVC . MainVC have a tableView with tableViewCell class.Every cell have a ShowButton .So what I want to do is,when I click on ShowButton , PopUpVC will show on top of MainVc

What I already do:

1) Connect MainVC and PopUpVC with segue of present modally,at the same time set the segue identifier to "popVc".

2) Set PopUpVC Transition Style = Cover Vertical and Presentation = Over Current Context , Background of PopUpVC I set to Clear Color in StoryBoard.

3) In TableViewCell class for the TableView,set up the protocol and delegate like below :

    //here is MyTableViewCell class
protocol MyDelegate : class {
    func showPopUpVc(itemId: Int)
}

class MyTableCell : UITableViewCell {

    weak var delegate : MyDelegate?


    @IBAction func showButtonTapped(_ sender: Any) {

        self.delegate?.showPopUpVc(itemId : MyItem.id)
    }
 }

 //View Controller 
class MainViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate , MyDelegate {

    func showPopVc(itemId; Int) {
        //here I want to show the PopUpVC
        self.performSegue(withIdentifier: "popVc", sender: self)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableCell", for: indexPath) as! MyTableCell
      cell.items = self.items[indexPath.row]
      cell.delegate = self
      return cell
}

}

4) So inside showPopVc() function in MainViewController I tried this 2 solution in order to show the popUpVC

//1st solution
func showPopVc(itemId; Int) { 
   self.performSegue(withIdentifier: "popVc", sender: self)
}

//2nd solution
func showPopVc(itemId; Int) { 
   let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let PopUpVc = storyBoard.instantiateViewController(withIdentifier: "PopUpVc")
    self.present(PopupVc, animated: true, completion: nil)
}

Output that I get now :

Whenever I tap the ShowButton , the tap is detected(Cause I set print("Clicked") in showPopUpVc() ), but the whole screen become black.None of the content in PopUpVC is shown on the screen.Just black.Cant go back to previous screen,cause is absolutely black and blank screen

Both solution in section 4 also produce the same output.

So what I missing here? Or can somebody show me a complete example to solve this problem?

Try this

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if(segue.identifier == "popVc")
    {

         let newVC = segue.destination;

        self.setPresentationStyleForSelfController(selfController: self,presentingController: newVC)


    }

 }

 func setPresentationStyleForSelfController(selfController:UIViewController,presentingController:UIViewController)
{
   if #available(iOS 8.0, *)
    {

        //iOS 8.0 and above

        presentingController.providesPresentationContextTransitionStyle = true;

        presentingController.definesPresentationContext = true;

        presentingController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext

    }
    else
    {

         presentingController.modalPresentationStyle = UIModalPresentationStyle.currentContext

        selfController.navigationController?.modalPresentationStyle = UIModalPresentationStyle.currentContext
    }

}

Set modal presentation style for view controller.

Try this and see (may this would work, programatically that one is not working with storyboard segue.):

func showPopVc(itemId; Int) { 
   let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let PopUpVc = storyBoard.instantiateViewController(withIdentifier: "PopUpVc")

    // Try anyone of these according to your requirement.
    PopUpVc.modalPresentationStyle = .overCurrentContext
    //PopUpVc.modalPresentationStyle = .overFullScreen

    self.present(PopupVc, animated: true, completion: nil)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM