2016-01-20 47 views
0

我有一個TableViewController,TableViewCell和一個ViewController。我在TableViewCell中有一個按鈕,我想用presentViewController來呈現ViewController(但ViewController在故事板上沒有視圖)。我試着使用:來自TableViewCell的presentViewController

@IBAction func playVideo(sender: AnyObject) { 
     let vc = ViewController() 
     self.presentViewController(vc, animated: true, completion: nil) 
} 

Error: Value of type TableViewCell has no member presentViewController


然後,我試過

self.window?.rootViewController!.presentViewController(vc, animated: true, completion: nil)

Error: Warning: Attempt to present whose view is not in the window hierarchy!


我在做什麼錯?我應該怎麼做才能從TableViewCell中呈現ViewController?另外我怎樣才能將數據傳遞給TableViewCell中的新呈現VC?


更新:

protocol TableViewCellDelegate 
{ 
    buttonDidClicked(result: Int) 
} 

class TableViewCell: UITableViewCell { 

    @IBAction func play(sender: AnyObject) { 
     if let id = self.item?["id"].int { 
      self.delegate?.buttonDidClicked(id) 
     } 
    } 
} 
---------------------------------------- 

// in TableViewController 

var delegate: TableViewCellDelegate? 
func buttonDidClicked(result: Int) { 
    let vc = ViewController() 
    self.presentViewController(vc, animated: true, completion: nil) 
} 

I receive error: Presenting view controllers on detached view controllers is discouraged

(請注意,我有背後的TableView的NavBar &的TabBar鏈)


我也試過

self.parentViewController!.presentViewController(vc, animated: true, completion: nil) 

同樣的錯誤。


也試過,

self.view.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil) 

同樣的錯誤

+0

該按鈕意味着是次要(例如,配件視圖)選擇​​嗎? – 2016-01-20 21:48:10

回答

4

您應該使用傳遞動作回tableViewController

1)在你的類創建一個

2)製作button行動打電話給你 FUNC

3)cell.delegate = self

4鏈接tableViewControllercell's protocol)實施cell's protocol,並添加您的代碼中有

let vc = ViewController() 
self.presentViewController(vc, animated: true, completion: nil) 
+0

我收到錯誤,不鼓勵在分離的視圖控制器上呈現視圖控制器。這是後話崩潰。我究竟做錯了什麼? – senty

+0

@senty您可以在問題中添加您的代碼,以便我可以看到您的代碼 – Breek

+0

我更新了問題並添加了當前代碼。你能檢查嗎? – senty

4

好像你已經有了這個想法爲了呈現視圖控制器,您需要一個視圖控制器。所以這裏是你需要做的:

  1. 創建一個協議,通知單元的控制器,該按鈕被按下。
  2. 在您的單元格中創建一個包含實現您的協議的委託的引用的屬性。
  3. 在按鈕操作中調用委託中的協議方法。
  4. 在視圖控制器中實現協議方法。
  5. 配置單元格時,請將視圖控制器作爲委託單元傳遞給單元格。

下面是一些代碼:

// 1. 
protocol PlayVideoCellProtocol { 
    func playVideoButtonDidSelect() 
} 

class TableViewCell { 
// ... 

// 2. 
var delegate: PlayVideoCellProtocol! 

// 3. 
@IBAction func playVideo(sender: AnyObject) { 
    self.delegate.playVideoButtonDidSelect() 
} 

// ... 
} 


class TableViewController: SuperClass, PlayVideoCellProtocol { 

// ... 

    // 4. 
    func playVideoButtonDidSelect() { 
     let viewController = ViewController() // Or however you want to create it. 
     self.presentViewController(viewController, animated: true, completion: nil) 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell { 
     //... Your cell configuration 

     // 5. 
     cell.delegate = self 

     //... 
    } 
//... 
} 
+0

使用此方法,我會收到:呈現視圖不鼓勵分離視圖控制器上的控制器。我沒有故事板文件的呈現ViewController() – senty

+0

我有一個預感,你會得到這個錯誤,如果你的TableViewController被呈現模態,而不是被推入導航堆棧。如果我被誤導了,請糾正我。 –

+0

它現在正在工作,但它看起來並不好(因爲我使用presentViewController然後dismissViewController,當我拉下來,但是當我拉下視圖,在該視圖後面它是全黑的,當它完全解散時,它顯示)所以關於你的評論,它的工作好嗎..我不知道其他案件presentViewController旁邊雖然... – senty

0

所以self.presentViewController從視圖控制器的方法。 你得到這個錯誤的原因是你所指的「自我」是tableViewCell。而tableViewCell沒有presentViewController的方法。

我覺得有一些可以使用的選項:1.新增 在細胞中的委託和協議,當你點擊該按鈕,IBAction爲將調用

​​

然後在你的tableVC,你只需要實現此方法並調用self.presentViewController

2.使用故事板和segue 在故事板中,從您的按鈕拖動到您想要的VC。

+0

這種方法並不能解決我的問題。請檢查我更新的問題 – senty