我在swift中編寫了ios應用程序。 我有一個ViewController持有自定義表視圖,其中包含自定義單元格。每個單元都有一個開關,在這個開關上激活,我希望視圖繼續到第二個視圖控制器。從swift中的自定義單元格按鈕接駁另一個視圖以用於iOS應用程序
我試圖通過聲明故事板變量,並instatiating第二個視圖控制器,並推到第二個視圖控制器來實現此目的。我已經搜索瞭解決方案,但是他們似乎都沒有工作。
在類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,無濟於事。 在本文中嘗試了使用懶惰屬性的建議 How to get global pointer to view controller in swift
我弄不明白我在做什麼錯了。我一直堅持了一下。