简体   繁体   English

当按下时,Segue将不会显示ViewController

[英]Segue won't show the ViewController when pressed

I currently have a ViewController with prototype cells in a UITable View . 我目前在UITable View有一个带有原型单元ViewController The cells currently display content from a Firebase DB when loaded. cells当前在加载时显示Firebase数据库中的内容。 What I would like to do is when a cell is pressed more information is shown from the Firebase DB . 我想做的是,当按下某个cell时, Firebase DB中会显示更多信息。 However, I currently cannot get the segue to push to the ViewController from the cell . 不过,我目前还不能得到segue ViewControllercell What should I do so this would work? 我应该怎么做才能正常工作?

Image of my Storyboard -- 我的分镜脚本图片-

在此处输入图片说明

These are two pieces of code that will help. 这是两段有用的代码。 For swift: 快速:

@IBAction func showEntries(_ sender: Any) {
    self.performSegue(withIdentifier: <sequeId>, sender: nil)
}

and a button with the action wrapping this. 和一个带有动作的按钮。

For Objective C, you can perform the segue like so. 对于目标C,您可以像这样执行segue。

[self performSegueWithIdentifier:@"showEditor" sender:self];

Create the action by CTRL and dragging the button or cell into the corresponding code file, select action and name it. 通过CTRL创建动作并将按钮或单元拖到相应的代码文件中,选择动作并命名。 在此处输入图片说明

It's hard to tell, but from the screenshot, it looks like the segue goes from controller to controller, rather than cell to controller. 很难说,但是从屏幕截图中可以看出,segue是从控制器到控制器的,而不是从单元到控制器的。 If you'd like to do it like this, then you need to give the segue an identifier (in the right panel of your screenshot) and then override the delegate method 如果您想这样做,则需要给segue一个标识符(在屏幕截图的右侧),然后覆盖委托方法

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "yourIdentifier", sender: self)
}

OR (if you don't feel like going to all this trouble)...in the storyboard, drag the segue from the cell to the view controller you want to push, rather than from controller to controller. 或(如果您不希望遇到所有麻烦)...在情节提要中,将segue从单元格拖动到要推送的视图控制器,而不是从控制器到控制器。

For example, 例如,

在此处输入图片说明

Notice how when the segue is selected, only the cell is outlined in blue, rather than the entire view controller. 请注意,当选择segue时,如何仅将单元格标记为蓝色,而不是整个视图控制器。

It's because cells are dynamic, so you can't assign an event handler from cell to uiviewcontroller. 这是因为单元是动态的,所以您不能将单元中的事件处理程序分配给uiviewcontroller。 Instead you should make the segue inside your cellForRowAtIndexPath method and push it from there. 相反,您应该在cellForRowAtIndexPath方法内部进行cellForRowAtIndexPath ,然后从那里推送它。 If you want to see the segue inside the storyboard nevertheless, then you can also ctrl+drag from viewcontroller1 to viewcontroller2 to make a new segue. 尽管如此,如果您仍想在情节提要板中查看序列,则也可以ctrl +拖动从viewcontroller1到viewcontroller2进行新的序列化。 Then select the segue and give it a Storyboard id. 然后选择segue,并为其指定一个Storyboard ID。 After That you can call self.performSegue(withIdentifier: "yourIdentifier") 之后,您可以调用self.performSegue(withIdentifier: "yourIdentifier")

I actually found the solution to this. 我实际上找到了解决方案。 The issue was fairly simple actually. 这个问题实际上很简单。 All I had to so was change the selection setting within the table view attributes from 'No Selection' to 'Single Selection'. 我所要做的就是将表视图属性内的选择设置从“无选择”更改为“单选”。

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

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