Screen transition destination does not appear as intended

Asked 5 months ago, Updated 5 months ago, 9 views

Based on Swift2's book called Swift+ios App Development Introduction, the following code created in Swift4 does not make the screen transition destination as intended.
In this code, the tableView on the initial screen displays the + button for new creation and a list of diary entries saved.The prepareForSegue method is called when you select one of them and use Segue to transition the screen, but by naming them addSegue and cellSegue, we try to separate the processing between the newly created and saved screen.
When I choose from the list of created and saved diaries, the diary recorded in cellSegue is intended to appear, but the new creation screen appears.The screen I created and saved does not appear.

①On the Main.storyboard, right-click on the inputViewController to create a segment and set the Identifier to cellSegue.At this time, there was something I couldn't do as the book said.It was because I couldn't create ManualSegue because it didn't respond even if I right-click on the inputViewController from the large square screen of tableView.So I clicked on the ViewController icon at the top of the large square screen of tableView, and I racked and dropped it to create a segment, and I turned the identifier into cellSegue.
I think this is the cause.But what should I do in this case?

コードI also think that there may be a problem if you choose swift4 in the code.What is the problem in this case?

import UIKit
import RealmSwift

classViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak variableView:UITableView!

    // Create a default realm instance
    let realm=try!Realm()
    // A list that stores data in DB, and automatically updates the list when sorted and updated
    let dataArray=try!Realm().objects(Diary.self).sorted(byKeyPath: "date", ascending: false)
    // Create an instance of `DateFormatter` and keep it in properties
    let dateFormatter: DateFormatter={
        let df = DateFormatter()
        // Use the system standard format .medium to match various settings (including time zones) on the time zone)
        return df

    override func viewDidLoad(){
        // Do any additional setup after loading the view, typically from anib.

    override funcdidReceiveMemoryWarning(){
        // Dispose of any resources that can be recreated.
    // Update table view when returning from input screen
    override func viewWillAppear(_animated:Bool){
    Called when screen transitions in //segue
    override func prepare(for segment:UIStoryboardSegue, sender:Any?) {
        let inputViewController: inputViewController=segue.destination as! inputViewController
        if segue.identifier == "cellSegue" {
            let indexPath=self.tableView.indexPathForSelectedRow
            inputViewController.diary=dataArray [indexPath!.row]

        } else{
            let diary=Diary()
            if dataArray.count!=0{

    // Returns the number of cells for each section
    functableView(_tableView:UITableView, numberOfRowsInSection section:Int) - > Int{
        return dataArray.count
// Cell Contents
    functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
// Reusable Cells
        letcell: UITableViewCell= UITableViewCell (style: UITableViewCellStyle.subtitle, reuseIdentifier: "Cell")
// Set cell value
        let object = dataArray [indexPath.row]

        // Change the line cell.detailTextLabel? as follows:
        cell.detailTextLabel?.text=dateFormatter.string( as Date)
        return cell
// that cells can be deleted
    functableView(_tableView:UITableView, editingStyleForRowAtindexPath:IndexPath)->UITableViewCellEditingStyle{
        return UITableViewCellEditingStyle.delete;

  // Run when each cell is selected
    functableView(_tableView:UITableView, didSelectRowAtindexPath:IndexPath){
        performSegue(withIdentifier: "cellSegue", sender:nil)

// Action to be taken when the delete button is pressed
    functableView(_tableView:UITableView, commit editingStyle:UITableViewCellEditingStyle, forRowAtindexPath:IndexPath){
                self.realm.delete (self.dataArray [indexPath.row])

            tableView.deleteRows(at:[indexPath], with:UITableViewRowAnimation.fade)



2022-09-30 11:01

1 Answers

The following code was missing from the inputViewController class override func viewDidLoad():


2022-09-30 11:01

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.