简体   繁体   中英

Segue won't show the ViewController when pressed

I currently have a ViewController with prototype cells in a UITable View . The cells currently display content from a Firebase DB when loaded. What I would like to do is when a cell is pressed more information is shown from the Firebase DB . However, I currently cannot get the segue to push to the ViewController from the cell . 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.

[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. 在此处输入图片说明

It's hard to tell, but from the screenshot, it looks like the segue goes from controller to controller, rather than cell to controller. 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

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.

For example,

在此处输入图片说明

Notice how when the segue is selected, only the cell is outlined in blue, rather than the entire view controller.

It's because cells are dynamic, so you can't assign an event handler from cell to uiviewcontroller. Instead you should make the segue inside your cellForRowAtIndexPath method and push it from there. 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. Then select the segue and give it a Storyboard id. After That you can call 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'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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