Jeff Bootsholz
Jeff Bootsholz

Reputation: 3068

assing object with prepareForSegue becomes nil in Swift 3

Sir,

I am trying to implement a form and pass the Data object below

import UIKit
import GRDB

class Staff: Record {
    var id: Int64?
    var compId: Int64 = 0 
     var chiName: String  = ""
    var engName: String  = ""

to the table view controller loading the child record. when it comes to implementation, it seems getting null and does not make sense. Would you please tell me how to ensure the second view controller does not receive null objects under this case ?

Below is the Log :

enter image description here Here is my code:

First UIView Controller

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    print("view salary X ")
    print(dummy)
    print(dummy.id ?? "0")

    if let secondController = segue.destination as? ViewSalaryTableViewController {
        secondController.dummyStaff = dummy
    }
} 

Second UITableView Controller :

  public var dummyStaff : Staff?

override func viewDidLoad() {
    super.viewDidLoad()
..
    print("arrive dummyStaff")
    print(dummyStaff ?? "njull")
}

Storyboard partial draft :

enter image description here

Storyboard setting

enter image description here

Upvotes: 0

Views: 151

Answers (1)

Achu22
Achu22

Reputation: 376

Make sure the type casting for secondController is working. If you have multiple segues, use segue identifier to distinguish. Below code worked fine for me:

class MyBook {
    var name:String!
}

ViewController 1

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "Vc1ToVc2" {
            let book = MyBook()
            book.name = "Harry"
            if let destinationVc = segue.destination as? ViewController2 {
                destinationVc.book = book
            }
        }
    }

ViewController 2

var book:MyBook?

    override func viewDidLoad() {
        super.viewDidLoad()
        print(book?.name ?? "No name")
    }

Prints: Harry

Upvotes: 2

Related Questions