【发布时间】:2018-02-02 09:06:23
【问题描述】:
我有 2 个视图控制器,MainVC 和 PopUpVC。 MainVC 有一个tableView 和tableViewCell 类。每个单元格都有一个ShowButton。所以我想做的是,当我点击ShowButton 时,PopUpVC 将显示在MainVc
我已经做了什么:
1) 将MainVC和PopUpVC与当前的segue连接,同时设置segue标识符为“popVc”。
2) 设置PopUpVC Transition Style = Cover Vertical 和 Presentation = Over Current Context ,PopUpVC 的背景我设置为清除颜色在 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