如何使用 Swift 中的 Objc 在 Table View with Button 中打开 View Controller?

How to open View Controller in Table View with Button by using Objc in Swift?

计算器

我知道如何在 table 视图单元格中制作一个带有网站链接、费率、邮件和许多内容的按钮。但是,如何在 @Objc 函数的语句中使用 instantiateViewController 打开视图控制器?

例如

创建一个名为 FeedBackButtonsTableViewCell

的新 Table View Cell 文件夹
class FeedBackButtonsTableViewCell: UITableViewCell {

    @IBOutlet weak var ButtonCells: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

让我们创建一个名为

的新视图控制器文件夹
class FeedbackViewController: UIViewController {


       @IBOutlet weak var TableView: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

          self.navigationItem.title = "Feedback"
    }

}

将调用视图控制器的扩展添加到 UITableViewDataSourceUITableViewDelegate 并在第二个 FeedbackViewController 中使用 UITableViewDataSource 和 [=19 创建一个 obj func 语句=] 在单元格下。

extension FeedbackViewController: UITableViewDataSource, UITableViewDelegate {

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1
    }

     if indexPath.row == 1 {

            buttonCell = TableView.dequeueReusableCell(withIdentifier: "ButtonCells") as? FeedBackButtonsTableViewCell

             buttonCell?.ButtonCells.addTarget(self,action: #selector(LearnMore),for: .touchUpInside)

             buttonCell?.ButtonCells.tag = indexPath.row

             return buttonCell!

        }

  @objc func LearnMore() {

    // How could I write to open the view controller with UIButton in the Table View Cells?

  }
}

感谢您带来的帮助! :)

简单的解决方案可能是使用协议。


protocol CellActionDelegate{
    func didButtonTapped(index: Int)
}

现在确认FeedbackViewController中的协议。在您的 UITableViewCell 子类中获取 indexactionDelegate 属性。

class FeedBackButtonsTableViewCell: UITableViewCell{

    var actionDelegate: CellActionDelegate?
    var index: Int?

    .....



    // Take Action of UIButton here

    @IBAction func more(_ sender: Any) {
      if let delegate = self.actionDelegate{

        delegate.didButtonTapped(index!)
      }
    }


}

现在在你的 FeedbackViewController 中设置 actionDelegate 和

中的相应索引
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {}

您可以从 func didButtonTapped(index: Int) 定义中打开另一个视图控制器。

extension FeedbackViewController:CellActionDelegate{
 func didButtonTapped(index: Int) {
        let storybord = UIStoryboard(name: "Main", bundle: nil)
        guard let controller = storybord.instantiateViewController(withIdentifier: "AnotherControllerIdentfier") as? AnotherViewController else{
            fatalError("Could not finc another view controller")
        }
        self.present(controller, animated: true, completion: nil)
    }
}

    @objc func LearnMore() {

        let viewController = FeedbackDetailsViewController()// creation of viewController object differs depends on how you fetch the UI, means either you are using storyboard or xib or directly making ui in code.
        self.navigationController?.pushViewController(viewController, animated: true)

      }