2014-12-27 16 views
1

我在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

我弄不明白我在做什麼錯了。我一直堅持了一下。

回答

1

故事板非常適合繼續使用。 1)在你的故事板創建2個控制器(我認爲它已經完成)

2)創建2個視圖控制器之間的賽格瑞(按住Ctrl +從第一視圖控制器到第二個點擊,它會問你你需要什麼樣的segue類型,選擇「show」)。

3)給出的識別符到該Segue公司(點擊鏈接和在第四標籤中的右圖)

4)在第一個視圖控制器,當所述開關被激活時,

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

用您選擇的標識符替換標識符「detail」。

相關問題