簡體   English   中英

從ios版SWIFT中的自定義單元格按鈕切換到另一個視圖

[英]Segueing to another view from a custom cell button in SWIFT for ios app

我正在迅速編寫一個ios應用程序。 我有一個ViewController持有一個自定義表格視圖,其中包含一個自定義單元格。 每個單元都有一個開關,在激活該開關時,我希望視圖可以連接到第二個視圖控制器。

我試圖通過聲明一個故事板變量,使第二個視圖控制器實例化,並推送到第二個視圖控制器來實現這一點。 我已經在董事會上尋找解決方案,但是似乎沒有一個可行。

在類customCellTableViewCell中:UITableViewCell

嘗試1:

customCellTableViewCell: UITableViewCell {

var storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let view2: viewController2 = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("View2") as viewController2
switchActive fnc(){

self.window?.rootViewController?.navigationController?.pushViewController(view2, animated: true)

}

…
//rest of code

錯誤:customCellTableViewCell.Type沒有名為故事板的成員

嘗試2:

customCellTableViewCell: UITableViewCell {

init(storyboard: UIStoryboard){
        self.storyboard = storyboard
}

required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
}

var storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let view2: viewController2 = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("View2") as viewController2
switchActive fnc(){

self.window?.rootViewController?.navigationController?.pushViewController(view2, animated: true)

}

…
//rest of code

錯誤:customCellTableViewCell.Type沒有名為故事板的成員

嘗試3:

customCellTableViewCell: UITableViewCell {


var storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let view2: viewController2 = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("View2") as viewController2

switchActive fnc(){

self.window?.rootViewController?.navigationController?.pushViewController(view2, animated: true)

}

…
//rest of code

將代碼放入switchActive fnc內,但不起作用。 用let代替var,無濟於事。 嘗試了這篇文章中的建議,使用了lazy屬性如何快速獲取全局指針以查看控制器

我無法弄清楚自己在做什么。 我被卡住了一段時間。

情節提要非常方便。 1)在情節提要中創建2個視圖控制器(我想已經完成了)

2)在2個視圖控制器之間創建一個segue(按住ctrl鍵並從第一個ViewController單擊到第二個ViewController,它將詢問您要使用的segue類型,選擇“顯示”)。

3)為該Segue提供一個標識符(單擊鏈接,然后單擊第4個標簽中的右側面板)

4)在您的第一個View Controller中,當開關被激活時,

self.performSegueWithIdentifier("detail", sender:nil)

用您選擇的標識符替換標識符“ detail”。

暫無
暫無

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

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