Screen transition destination does not appear as intended

Asked 2 years ago, Updated 2 years ago, 29 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)
        df.dateStyle=.medium
        df.timeStyle=.medium
        return df
    }()


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

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    // Update table view when returning from input screen
    override func viewWillAppear(_animated:Bool){
        super.viewWillAppear(animated)
        tableView.reloadData()
    }
    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()
            diary.title="Title"
            diary.body="body"
            if dataArray.count!=0{
                diary.id=dataArray.max(ofProperty: "id")!+1
            }
            inputViewController.diary=diary
        }
    }

    // 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]
        cell.textLabel?.text=object.title

        // Change the line cell.detailTextLabel?.text=object.date.description as follows:
        cell.detailTextLabel?.text=dateFormatter.string(from:object.date 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){
        ifeditingStyle==UITableViewCellEditingStyle.delete{
            try!realm.write{
                self.realm.delete (self.dataArray [indexPath.row])

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

}

swift

2022-09-30 11:01

1 Answers

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

 titleTextField.text=diary.title
bodyTextView.text=diary.body


2022-09-30 11:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.