简体   繁体   English

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

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

How can I perform segue from one .xib (TableCell.xib) to another .xib(UIViewController) on click on TableCell? 如何在单击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...
  }

First, I tried setting up a manual segue in Storyboard. 首先,我尝试在Storyboard中设置手动segue。 Note that, TableView & TableViewCell are external xib but parent of TableView is in Storyboard. 请注意,TableView和TableViewCell是外部xib,但TableView的父级是在Storyboard中。 So, I tried control-dragging from parent-ViewController to the detailViewController (and identifier: showDetails)..However, doesn't work. 所以,我尝试从parent-ViewController控件拖动到detailViewController(和标识符:showDetails)。但是,不起作用。

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

Obviously, I received this error (because TableView is in .xib whereas parent and detailsVC are in Storyboard and I ctrl+dragged from parentVC to detailsVC but TableView1 apparently doesn't recognise the segue in Main Storyboard): 显然,我收到了这个错误(因为TableView在.xib中,而parent和detailsVC在Storyboard中,我按住Ctrl +从parentVC拖到detailsVC但TableView1显然无法识别主故事板中的segue):

TableView1: 0x7f97626a3280>) has no segue with identifier 'showDetails'' TableView1:0x7f97626a3280>)没有标识符'showDetails'的segue'


Then I tried creating a ViewController and tried adding var parentNavigationController : UINavigationController! 然后我尝试创建一个ViewController并尝试添加var parentNavigationController : UINavigationController! at the top and referencing below chunk inside didSelectRowAtIndex 在顶部并在didSelectRowAtIndex内引用下面的块

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

     parentNavigationController!.pushViewController(newVC, animated: true)

However, I am receiving an error: 但是,我收到一个错误:

DetailsVC: 0x7ff7794ae550> fatal error: unexpectedly found nil while unwrapping an Optional value DetailsVC:0x7ff7794ae550>致命错误:在解包可选值时意外发现nil


Finally, I also tried below chunk inside didSelectRowAtIndex , and got something close (as I can use dismissView to < Back ); 最后,我还在didSelectRowAtIndex尝试了下面的块,并得到了一些接近(因为我可以使用dismissView来< Back ); but not exactly what I want. 但不完全是我想要的。 Its problem is that the segue animation looks like the page is coming from bottom. 它的问题是segue动画看起来像页面来自底部。

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

I am also receiving a warning (but not crash) for some reason I couldn't figure out: 我也收到一个警告(但没有崩溃)由于某种原因我无法弄清楚:

Presenting view controllers on detached view controllers is discouraged 不鼓励在分离的视图控制器上呈现视图控制器


  • Which approach is the right one? 哪种方法合适? Or is there a better way to adapt to achieve what I am trying to? 或者有更好的方法来适应我想要达到的目标吗?

  • Firstly, what is the right way to perform segue from didSelectRowAtIndex (in TableView.xib class) to a new ViewController (either Storyboard or xib)? 首先,从didSelectRowAtIndex (in TableView.xib class)执行segue到新的ViewController(Storyboard或xib)的正确方法是什么?

  • Secondly, assume that there is an image thumbnail in the TableViewCell.xib . 其次,假设TableViewCell.xib中有一个图像缩略图。 What is right way of performing segue to a new UIView on click on that view? 在点击该视图时,对新UIView执行segue的正确方法是什么? (like full-screen image / dismiss-able) (如全屏图像/可忽略)

Your second approach looks to be right one but of course you are doing something wrong there. 你的第二种方法看起来是正确的,但当然你在那里做错了。 You need to drag and drop Segue from Parent ViewController to Detail ViewController and give it a Segue identifier in Storyboard (check attached Image below). 您需要将Segue从Parent ViewController拖放到Detail ViewController并在Storyboard中为其指定Segue标识符(请查看下面附带的图像)。 IN didSelectRowAtIndexPath you need to do below code: 在didSelectRowAtIndexPath中你需要做下面的代码:

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

As I can see you already tried that but it looks like you missed some step either setting Segue Identifier or giving wrong identifier in performSegueWithIdentifier method. 我可以看到你已经尝试过,但看起来你错过了一些设置Segue Identifier或在performSegueWithIdentifier方法中给出错误标识符的步骤。 Also your Parent ViewController should be embedded in UINavigationController in case you are missing that thing :) 你的Parent ViewController也应该嵌入UINavigationController,以防你错过那个东西:) 在此输入图像描述

Also keep in mind you should perform navigation (here its Segue) from one ViewController to another ViewController at same level not from one View to another ViewController. 另外请记住,你应该从一个ViewController到同一级别的另一个ViewController而不是从一个View到另一个ViewController执行导航(这里是它的Segue)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM