通过在集合视图单元格内按按钮实例化情节提要

时间:2018-12-13 21:39:46

标签: ios swift uitableview tableview

我正在尝试通过在集合视图单元格中按下按钮来实例化情节提要,但遇到错误:

class sampleCell: UICollectionViewCell {

    @IBAction func infoButtonPressed(_ sender: Any) {

        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InputViewController") as! InputViewController//error: Use of undeclared type "InputViewController"

        present(vc, animated: false)//error:Use of unresolved identifier 'present'
    }
}

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

1-您没有名为InputViewController

的课程
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InputViewController") as! InputViewController 

2-您不能在单元格中显示vc,需要委托

present(vc, animated: false) 

仅用于主情节提要的注释

UIStoryboard(name: "Main", bundle: nil)

with(但也可以在vc中)

self.storyboard

修改:-------------------------------------- -------------------

在单元格内

class sampleCell: UICollectionViewCell {
  weak var delegate:CurrentController?
}

然后在cellForItemAt

let cell = /////
cell.delegate = self

添加到vc

func goToInput() {

    let vc = self.storyboard!.instantiateViewController(withIdentifier: "InputViewController") as! InputViewController//error: Use of undeclared type "InputViewController" 
    present(vc, animated: false) 

}

此后,内部细胞动作

@IBAction func infoButtonPressed(_ sender: Any) {
  delegate?.goToInput()
}

答案 1 :(得分:0)

您遇到了错误,因为present(_:animated:completion:)是UIViewController实例方法,而不是UITableViewCell。

因此,您应该寻找一种方法来处理包含表视图的视图控制器中的按钮动作,而不是尝试从单元格类中呈现视图控制器。

考虑您将遵循以下答案:https://stackoverflow.com/a/28895402/5501940

您可以在按钮选择器方法中调用present方法:

@objc func buttonTapped(sender: UIButton) {
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InputViewController") as! InputViewController
    present(vc, animated: false)
}

还要确保您有一个InputViewController视图控制器。