Swift:尝试推送到现有视图控制器时“未声明类型的使用”

时间:2018-09-07 16:53:20

标签: swift xcode viewcontroller

所以我试图以编程方式推送到情节提要上的现有视图控制器,但是我得到了使用未声明的类型:LoginViewController,即使我将情节提要ID设置为LoginViewController也是我的代码:

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.
    }
}

提前谢谢!

1 个答案:

答案 0 :(得分:1)

确保您创建一个继承自UIViewController的新类LoginViewController

为此,请转到文件菜单> 新建> 文件

new file

iOS 标签中,选择 Cocoa Touch Class

New Class

然后下一步

给出类名LoginViewController,并使其成为UIViewController的子类

class name

点击下一步,然后点击创建。这样会将新文件添加到您的项目中,然后您可以/应该自定义其内容。

在情节提要中的身份检查器标签中,确保将类名称和标识符设置为LoginViewController

Storyboard