【问题标题】:Switch View Controller From TableViewCell Class in Swift从 Swift 中的 TableViewCell 类切换视图控制器
【发布时间】:2019-04-21 08:49:32
【问题描述】:

当用户单击 tableviewcell 中的元素时,我想呈现一个新的 ViewController。然而,用于启动 VC 的标准代码在 tableview 单元格中甚至在帮助器类中都不起作用,因为 TVC 和帮助器类都不能呈现视图控制器。

这是帮助类中的代码。无论是放在 helperclass 还是 tableview 单元格中,它都没有启动 VC 的当前方法。

class launchVC {
 func launchVCNamed(identifier: String) {
    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let secondVC = storyBoard.instantiateViewController(withIdentifier: "contactDetail")
//FOLLOWING LINE HAS ERROR NO SUCH MEMBER (present)
    self.present(secondVC, animated: true, completion: nil)
    }
 }

如何修改它以启动 VC?

【问题讨论】:

    标签: ios swift presentviewcontroller


    【解决方案1】:

    通常您应该使用委托模式或闭包将块从单元格传回视图控制器。我更喜欢对委托使用闭包,所以我将给出这样的例子:

    class SomeCell: UITableViewCell {
        var actionBlock = { }
    
        func someActionOccured() { // some action like button tap in cell occured
            actionBlock()
        }
    }
    

    在视图控制器的cellForRow 中,您需要分配闭包

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SomeCell // replace cell identifier with whatever your identifier is
        cell.actionBlock = { [unowned self] in 
            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let secondVC = storyBoard.instantiateViewController(withIdentifier: "contactDetail")
            self.present(secondVC, animated: true, completion: nil)
        }
        return cell
    }
    

    【讨论】:

      【解决方案2】:

      向您的单元格添加一个委托并将其分配给presentingVC。见下文。

      使用委托

      创建一个继承自 UITableViewCell 的 CustomCell。

      class CustomCell: UITableViewCell {
          var cellDelegate : CustomCellDelegate = nil
      
          @IBAction func elementTapped() {
              cellDelegate?.launchVC()
          }
      }
      

      自定义单元委托

      protocol CustomCellDelegate {
          func launchVC()
      }
      

      MainViewController

      class ViewController: UIViewController, UITableViewControllerDataSource, UITableViewDelegate {
      
          IBOutlet weak var tableView: UITableView!
      
          override func viewDidLoad() {
              tableView.dataSource = self
              tableView.delegate = self
          }
      
          func numberOfSections(in: UITableView) -> Int {
              return 1
          }
      
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return 1
          }
      
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? CustomCell {
      
                  // important
                  cell.delegate = self
                  return cell
              }
          }
      }
      

      扩展 ViewController 以实现协议

      extension ViewContrller: CustomCellDelegate {
          func launchVC() {
              let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
              let vc = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
              self.present(vc, animated: true, completion: nil)
          }
      }
      

      【讨论】:

      • 抱歉,如果您尝试从 TableViewCell 呈现 VC,@olejnjak 有正确的实现。
      猜你喜欢
      • 2021-11-25
      • 1970-01-01
      • 2016-08-02
      • 2011-06-05
      • 2015-07-24
      • 2015-04-20
      • 2012-04-26
      • 2018-04-22
      相关资源
      最近更新 更多