Problem opening another viewcontroller

im having a problem where my function to open a second view controller isn’t working and is just crashing.
here is the code that crashes it:

        let NewRecipe = self.storyboard?.instantiateViewController(withIdentifier: "NewRecipeVC")as! NewRecipe
        present(NewRecipe,animated: true)

it is giving me an error:

Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1859c41a8)

All I’m trying to do is open the view controller called “NewRecipeVC”

Try changing your code to this:

let vc = self.storyboard?.instantiateViewController(withIdentifier: "NewRecipeVC")as! NewRecipe
present(vc ,animated: true)

I’m wondering if Xcode is struggling to diffrentiate between NewRecipe in your let statement and NewRecipe in your cast statement. Also it’s possible that the complier is also confused where you have used NewRecipe in your present() statement.

@Chris_Parker
ok before i was forgetting to call the function that triggers that code but now that it is triggering it is giving me a different error

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

the code i have written in looks like this now

    func createrecipe(){
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "NewRecipeVC")as! NewRecipe
        present(vc,animated: true)

    }

}

What happens if you adjust this:

withIdentifier: "NewRecipeVC")as! NewRecipe

by putting in a space here:

withIdentifier: "NewRecipeVC") as! NewRecipe

Also, is your ViewController named NewRecipe or NewRecipeVC? Is it a deliberate mismatch between the identifier and the class name?

@roosterboy i made sure they are named the same but i will try adding that space

@roosterboy still giving me that same error

I’ve just cobbled up some code to check that what you are doing makes sense and as long as you have the name correct in your Storyboard ID (the Identifier) for that target ViewController it should work.

I’m a bit fussy about naming things and I would have named that NewRecipe to be NewRecipeViewController but that’s your choice. It just makes it clear as to what it is.

So in my calling ViewContoller I have a simple Button that you tap and present it:

class ViewController: UIViewController {

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

    @IBAction func newRecipeTapped(_ sender: Any) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "newRecipeVC") as? NewRecipeViewController {
            present(vc, animated: true)
        }

    }

}

In the NewRecipeViewController I have a label in the middle so I know that I have the right one and the view.backgroundColor = .red

class NewRecipeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = .red
    }
}

@Chris_Parker i have changed it all to look the same as yours, i think i might swap it over to a fresh project, i have only written about 50 lines. and i was messing around with stuff earlier on it to see if i could get it to work myself.ill reply back to this convo if it works on a new project

@Ru55_2
Before you throw the original project in the bin, can you try cleaning it (Shift + Command + K) and see if that restores some kind of sensibility?

@Chris_Parker i tried with the new project and it didn’t seem to work

@Chris_Parker i tried doing the Shift + command + K on the new project, but I’m still getting the same error.
do you think that other things i wrote are causing the error?
this is the rest of my code i have:

import Firebase
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        text.backgroundColor = .white
         text.textColor = .black
         text.autocapitalizationType = .words
         text.clearsOnBeginEditing = true

    }
    let db = Firestore.firestore()

    @IBOutlet weak var text: UITextField!
    
    
    @IBAction func SearchButton(_ sender: Any) {
        text.resignFirstResponder()
        Check(name: text.text!)
    }
    
    func Check(name : String){
        let recipes = db.collection("Recipe")
        let query = recipes.whereField("recipes", in: [name])
        
        query.getDocuments { (querySnapshot, error) in
            if let error = error {
                print(error.localizedDescription)
            } else {
                if querySnapshot!.documents.isEmpty {
                    print("No data Found")

                    self.createrecipe()
                } else {
                    // Loop through documents and print data.
                    for doc in querySnapshot!.documents {
                        print(doc.data())
                    }
                }
            }
        }
    }
    
    
    func createrecipe(){
        if let vc = self.storyboard?.instantiateViewController(withIdentifier: "NewRecipeVC")as? NewRecipeViewController{
            present(vc,animated: true)
    }
    }

}```

Can you share your project via DropBox or GoogleDrive and I’ll take a look for you. I’m just at home convalescing so something to keep me occupied is welcome.

here is the link to the project file Google Drive: Sign-in

tell me once you’ve opened it then i can delete the link

It’s not public so you have to allow access.

You should get an email I think.

@Ru55_2

Check your emails for a request for access from Google.

@Ru55_2

Just in the process of downloading it. Taking a while since Google has to compress it into a zip file.

1 Like

OK got it.