0
votes

In my program when a button is pressed I am adding information to a database, including creating invoice number then calling a segue to a new view controller. When the new view controller is called I'd like to pass along that invoice number. Everything works fine, I can pass along sample data no problem. However, it appears that "override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject!) {}" is being called before my button (upon initialization of the view controller?), so I am passing along a blank value. How can I make my prepareForSegue wait till after my button is pressed? Here is the code I currently have.

@IBAction func createInvoice(sender: AnyObject) {
     let realm = Realm()
    let invoicepull = Invoice()
    let invoicecount = realm.objects(Invoice)
    let invoicenraw = invoicecount.count
    let a = 100
    let invoicenumber = a + invoicenraw
    var invoicefile = Invoice()
    invoicefile.inumber = invoicenumber
    invoicefile.cnumber = clientcombo.stringValue
    invoicefile.cost = owed.doubleValue
    invoicefile.paid = paid.doubleValue
    invoicefile.sevicecode = service.stringValue
    invoicefile.dateofservice = NSDate()
    // Save your object
    realm.beginWrite()
    realm.add(invoicefile)
    realm.commitWrite()
    //Sent notification
    performSegueWithIdentifier("cinvoiceseuge", sender: nil)
 println("Inside Action")
    println(invoicenumber)
    dismissViewController(self)
}

override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "cinvoiceseuge") {
        //Checking identifier is crucial as there might be multiple
        // segues attached to same view
        var detailVC = segue.destinationController as! invociegenerator;
        detailVC.toPass = invoicenumber
        println("Inside Sugue")
        println(invoicenumber)
    }
} 

Update: I belive this is an issue with the Realm database causing it to behave unexpectedly. If I remove all realm code, the program works as expected and I can pass a static dummy value.

1
I don't understand "wait till after my button is pressed". Isn't createInvoice called because the button was pressed? - Phillip Mills
@Phillip Mills Yes, so when the button is pressed createinvoice is called and a invoice number is generated, but prepareforSugue is being called before createinvoice so it can not send along the value, because it was not created yet. - Dan DeLuca
That should only happen if you have the segue connected to the button in your storyboard instead of to the view controller itself. However, if that were true you should also get an error when you call performSegueWithIdentifier. Can you try logging messages at the beginning of each method to see the exact sequence? - Phillip Mills
Hmm well perhaps I am missing something. The sugue is attached to the view controllers, I have no errors. When debugging with 'println()' statements I get. 'Inside Sugue 0 Second View Controller 0 Inside Action 107' Showing the second view controller is being opened before I even get an invoice number?? - Dan DeLuca
I can't promise this will help but it would be interesting to see the result of having NSLog("prepareForSegue: %@", NSThread.callStackSymbols()) where you currently print "Inside Sugue 0". It might show why the segue is being triggered early. - Phillip Mills

1 Answers

0
votes

invoicenumber in createInvoice() is a local variable and invoicenumber in prepareForSegue() seems to be an instance variable. is it what you expected?