簡體   English   中英

通過在集合視圖單元格內按按鈕實例化情節提要

[英]instantiate a storyboard from a button press inside a collection view cell

我正在嘗試通過在集合視圖單元格中按下按鈕來實例化情節提要,但遇到錯誤:

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

任何想法如何做到這一點?

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)

與(但在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()
}

您面臨的錯誤是因為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視圖控制器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM