我已经创建了一个简单的应用,该应用使用情节提要来演示新的页面/课程。但是,我想通过编程来做同样的事情。
我的初始ViewController.swift文件如下所示:
JQuery
当我按下按钮时,出现以下错误:
由于未捕获的异常而终止应用程序 “ NSInvalidArgumentException”,原因:“ Storyboard()不包含带有标识符的视图控制器 'SelectionScreen'
我使用Cocoa Touch Class创建了下一个Viewcontroller:
import UIKit
class ViewController: UIViewController {
var mainImageView: UIImageView!
var chooseButton: UIButton!
var nameLabel: UILabel!
override func loadView() {
view = UIView()
view.backgroundColor = .white
let chooseButton = UIButton(type: .custom) as UIButton
chooseButton.backgroundColor = .blue
chooseButton.layer.borderColor = UIColor.darkGray.cgColor
chooseButton.layer.borderWidth = 2
chooseButton.setTitle("Pick a side", for: .normal)
chooseButton.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
chooseButton.addTarget(self, action: #selector(clickMe), for: .touchUpInside)
chooseButton.layer.cornerRadius = chooseButton.frame.size.height/2
self.view.addSubview(chooseButton)
self.nameLabel = UILabel()
nameLabel.text = "Here is your side"
nameLabel.textAlignment = .center
nameLabel.backgroundColor = .cyan
nameLabel.frame = CGRect(x: 100, y: 400, width: 200, height: 100)
self.view.addSubview(nameLabel)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@objc func clickMe(sender:UIButton!) {
print("Button Clicked")
self.nameLabel.text = "updated title"
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let loadVC = storyBoard.instantiateViewController(withIdentifier: "SelectionScreen") as! SelectionScreen
self.present(loadVC, animated: true, completion: nil)
}
当我最初使用Storyboard进行此操作时,我使用Identity Inspector设置Storyboard ID,以便InstantiateViewController(withIdentifier :)可以加载我要查看的新类。如何在不使用情节提要的情况下为SelectionScreen类提供标识符?