簡體   English   中英

執行Segue - TableViewCell.xib到UIView - 和 - TableView.xib到UIViewController -

[英]Performing Segue from - TableViewCell.xib to UIView - and - TableView.xib to UIViewController -

如何在單擊TableCell時從一個.xib(TableCell.xib)到另一個.xib(UIViewController)執行segue?

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(results?[indexPath.row])  // prints successfully
      ... 
      // apply below chunks here...
  }

首先,我嘗試在Storyboard中設置手動segue。 請注意,TableView和TableViewCell是外部xib,但TableView的父級是在Storyboard中。 所以,我嘗試從parent-ViewController控件拖動到detailViewController(和標識符:showDetails)。但是,不起作用。

      ...
        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
            self.performSegueWithIdentifier("showDetails", sender: AnyObject?())
        })
      ...

顯然,我收到了這個錯誤(因為TableView在.xib中,而parent和detailsVC在Storyboard中,我按住Ctrl +從parentVC拖到detailsVC但TableView1顯然無法識別主故事板中的segue):

TableView1:0x7f97626a3280>)沒有標識符'showDetails'的segue'


然后我嘗試創建一個ViewController並嘗試添加var parentNavigationController : UINavigationController! 在頂部並在didSelectRowAtIndex內引用下面的塊

   ...
     let newVC : UIViewController = CellDetailsVC()
     newVC.title = "DetailsView"
     print(newVC)

     parentNavigationController!.pushViewController(newVC, animated: true)

但是,我收到一個錯誤:

DetailsVC:0x7ff7794ae550>致命錯誤:在解包可選值時意外發現nil


最后,我還在didSelectRowAtIndex嘗試了下面的塊,並得到了一些接近(因為我可以使用dismissView來< Back ); 但不完全是我想要的。 它的問題是segue動畫看起來像頁面來自底部。

   ...
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("VideoDetailsVC") as UIViewController
    self.presentViewController(vc, animated: true, completion: nil)
 }

我也收到一個警告(但沒有崩潰)由於某種原因我無法弄清楚:

不鼓勵在分離的視圖控制器上呈現視圖控制器


  • 哪種方法合適? 或者有更好的方法來適應我想要達到的目標嗎?

  • 首先,從didSelectRowAtIndex (in TableView.xib class)執行segue到新的ViewController(Storyboard或xib)的正確方法是什么?

  • 其次,假設TableViewCell.xib中有一個圖像縮略圖。 在點擊該視圖時,對新UIView執行segue的正確方法是什么? (如全屏圖像/可忽略)

你的第二種方法看起來是正確的,但當然你在那里做錯了。 您需要將Segue從Parent ViewController拖放到Detail ViewController並在Storyboard中為其指定Segue標識符(請查看下面附帶的圖像)。 在didSelectRowAtIndexPath中你需要做下面的代碼:

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

我可以看到你已經嘗試過,但看起來你錯過了一些設置Segue Identifier或在performSegueWithIdentifier方法中給出錯誤標識符的步驟。 你的Parent ViewController也應該嵌入UINavigationController,以防你錯過那個東西:) 在此輸入圖像描述

另外請記住,你應該從一個ViewController到同一級別的另一個ViewController而不是從一個View到另一個ViewController執行導航(這里是它的Segue)。

暫無
暫無

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

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