简体   繁体   中英

Segue from bar button item in navigation bar to another view controller won't run my code?

I'm having problems with my segues in my app.

I added a bar button item to my navigation bar and ctrl dragged it to another view controller which works as expected when tested. I ctrl dragged it again this time making it an Action in its own VC and added code.

What I want to happen when it is pressed as well as performing the segue but only the segue happens. If I delete the segue then the code runs but I can't get both to work together like I was expecting.

Here is my code:

import UIKit
import CoreData

class NewClientViewController: UIViewController, UITextFieldDelegate {

    var managedObjectContext: NSManagedObjectContext!

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var ageTextField: UITextField!
    @IBOutlet weak var telephoneTextField: UITextField!
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var heightTextField: UITextField!
    @IBOutlet weak var weightTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    }

    // Dismiss keyboard when empty space tapped
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        nameTextField.endEditing(true)
        ageTextField.endEditing(true)
        telephoneTextField.endEditing(true)
        emailTextField.endEditing(true)
        heightTextField.endEditing(true)
        weightTextField.endEditing(true)
    }

    // Dismiss keyboard when return tapped
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {

        textField.resignFirstResponder()

        return true
    }

    @IBAction func saveButton(_ sender: Any) {

        let clientItem = Client(context: managedObjectContext)

        clientItem.name = nameTextField.text
        clientItem.age = ageTextField.text
        clientItem.telephone = telephoneTextField.text
        clientItem.email = emailTextField.text
        clientItem.height = heightTextField.text
        clientItem.weight = weightTextField.text

        do
        {
            try self.managedObjectContext.save()
            print("Successfully Saved!")
        }
        catch
        {
            print("Could not save data \(error.localizedDescription)")
        }

    }

}

Drag a segue from your firstViewController to your secondViewController instead and add an identifier to it instead. And then in your button action you just do this:

performSegue(withIdentifier: "yourIdentifier", sender: nil)

Add the below function if you want to pass any values:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "yourIdentifier" {
        let secondViewController = segue.destination as! SecondViewController
    }
}

I was still having trouble and managed to fix it using the

performSegue(withIdentifier: "yourIdentifier", sender: nil)

But it had to be from the VC i was wanting to come from as i'd have expected.

The issue that was stopping this happening properly was the button. When i created it up in the nav bar i wouldn't get the touch up inside so to overcome this i made the button and just placed it on the VC not it's nav bar, I linked it up as normal and added my code then dragged it into the nav bar and everything worked!

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