【问题标题】:How to present a separate View Controller when click on a button inside a custom TableViewCell Swift 4?单击自定义 TableViewCell Swift 4 中的按钮时如何呈现单独的视图控制器?
【发布时间】:2018-02-02 09:06:23
【问题描述】:

我有 2 个视图控制器,MainVCPopUpVCMainVC 有一个tableViewtableViewCell 类。每个单元格都有一个ShowButton。所以我想做的是,当我点击ShowButton 时,PopUpVC 将显示在MainVc

我已经做了什么:

1)MainVCPopUpVC与当前的segue连接,同时设置segue标识符为“popVc”。

2) 设置PopUpVC Transition Style = Cover VerticalPresentation = Over Current ContextPopUpVC 的背景我设置为清除颜色在 StoryBoard 中。

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

    //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) 所以在MainViewController 中的showPopVc() 函数内我尝试了这2 个解决方案以显示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时,都会检测到点击(因为我在showPopUpVc()中设置了print("Clicked")),但是整个屏幕变黑了。屏幕上没有显示PopUpVC中的内容。只是black.不能返回上一个屏幕,因为绝对是黑屏和空白屏幕

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

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

【问题讨论】:

    标签: ios iphone uitableview swift4


    【解决方案1】:

    试试这个

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

    【讨论】:

    • 使用//第一个解决方案将弹出窗口的背景颜色设置为从情节提要透明
    • 我已经在 StoryBoard 中将popUpVc 的背景设置为清除颜色
    • 并且反对票不是来自我的 ya..tq 寻求您的帮助
    • 检查您的弹出视图DidLoad,您可能会更改背景颜色或执行退货声明
    • 重新检查segue名称
    【解决方案2】:

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

    试试这个看看(也许这会起作用,以编程方式,一个不使用故事板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)
    }
    

    【讨论】:

    • 兄弟我试过你的代码,但是一旦点击按钮,整个屏幕还是黑屏。我检查了我的PopUpVc的标识符,是正确的。不知道为什么会这样
    • PopUpVc 是一个视图控制器。 MainVc 和 PopUpVc 都是 View Controller。并且不涉及任何 NavigationController
    • 我以模态方式将 MainVc 和 PopVc 与 segue 连接,将 segue 的标识符设置为“popVc”这个我检查了很多次。所以我使用self.performSegue(withIdentifier: "popVc", sender: self),黑屏。我已经三次检查的标识符是“popVc”不知道什么问题
    • 我将当前模态序列从 MainVc 附加到 PopUpVc.View 控制器到视图控制器
    • 好的,我现在就做..5 分钟
    猜你喜欢
    • 2011-02-11
    • 2019-11-13
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多