单击自定义TableViewCell Swift 4中的按钮时如何呈现单独的视图控制器?

时间:2018-02-02 09:06:24

标签: ios iphone uitableview swift4

我有2个视图控制器,MainVCPopUpVCMainVCtableViewtableViewCell类。每个单元格都有一个ShowButton。所以我想要做的是,当我点击ShowButton,{{ 1}}将显示在PopUpVC

之上

我已经做了什么:

1)以模态方式将MainVcMainVC与当前的segue连接,同时将segue标识符设置为" popVc"。

2)设置PopUpVC 转换样式=封面垂直演示文稿=当前上下文,{{1}的背景我在StoryBoard中设置为清除颜色

3)在TableView的TableViewCell类中,设置协议和委托,如下所示:

PopUpVC

4)因此PopUpVC //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 } } 内的showPopVc()函数中我尝试了这个2解决方案以显示MainViewController

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) } 时,都会检测到点按(因为我在ShowButton中设置了print("Clicked")),但整个屏幕变为黑色。{{1}中没有任何内容屏幕上显示。只是黑色。无法返回上一屏,原因是绝对黑屏和空白

第4节中的两个解决方案也产生相同的输出。

那么我在这里错过了什么?或者有人能给我一个完整的例子来解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

试试这个

 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
    }

}

答案 1 :(得分:0)

为视图控制器设置模式演示样式。

试试这个并看看(这可能会起作用,以编程方式表示那个人没有使用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)
}