Swift: “Use of undeclared type” when trying to push to existing view controller
Swift: “Use of undeclared type” when trying to push to existing view controller
so I'm trying to push to existing view controller on the storyboard programmatically ,but I'm getting Use of undeclared type: LoginViewController even that I set the Storyboard ID to LoginViewController here is my code:
import UIKit
class ViewController: UIViewController
@IBOutlet weak var myButton: UIButton!
@IBAction func Button(_ sender: UIButton)
if let ButtonImage = myButton.image(for: .normal),
let Image = UIImage(named: "ButtonAppuyer.png"),
UIImagePNGRepresentation(ButtonImage) == UIImagePNGRepresentation(Image)
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(loginVC, animated: true)
else
print("OK")
override func viewDidLoad()
super.viewDidLoad()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
Thank you in advance!
1 Answer
1
Make sure you create a new class LoginViewController that inherits from UIViewController.
LoginViewController
To do so go to the File menu > New > File

In the iOS tab, choose Cocoa Touch Class

And then Next
Give the class name LoginViewController, and make it a subclass of UIViewController
LoginViewController
UIViewController

Click Next and then Create. That will add a new file to your project, and you could/should customize its contents later.
In your storyboard, in the Identity Inspector tab, make sure to set the class name and the identifier to LoginViewController.
LoginViewController

Thank you I’m completely new could you please show me example in the code? Thank you!
– andreydimitro
Sep 7 '18 at 17:11
I got it. Thank you!
– andreydimitro
Sep 7 '18 at 17:32
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what is LoginViewController? is it defined for your target?
– giorashc
Sep 7 '18 at 17:05