Swift: 如何访问导航控制器后的第二个视图?
Swift: How to access second view after navigation controller?
我是 swift 的新手,目前正在开发一个问答应用程序项目。现在我有了它,所以在按下开始按钮后,它会推开当前的视图控制器并向用户显示一个新的视图控制器(vc),但我不确定 how/where 添加文本字段和用户在按下开始后看到的新视图 (vc) 的按钮。
let vc = UIViewController()
vc.view.backgroundColor = .red
navigationController?.pushViewController(vc, animated: true)
您必须创建另一个包含 UI 的视图控制器。
class SecondViewController: UIViewController {
private let textView = UITextView()
func viewDidLoad() {
super.viewDidLoad()
// Add text view with auto layout
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
textView.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
}
然后在尝试推送新的视图控制器时使用此 SecondViewController
let vc = SecondViewController()
vc.view.backgroundColor = .red
navigationController?.pushViewController(vc, animated: true)
您可以使用指定的标识符声明视图控制器
let vc = UIStoryboard.init(name: "Authentication", bundle: Bundle.main).instantiateViewController(withIdentifier: "Login") as? LoginVC
self.navigationController?.pushViewController(vc!, animated: true)
我是 swift 的新手,目前正在开发一个问答应用程序项目。现在我有了它,所以在按下开始按钮后,它会推开当前的视图控制器并向用户显示一个新的视图控制器(vc),但我不确定 how/where 添加文本字段和用户在按下开始后看到的新视图 (vc) 的按钮。
let vc = UIViewController()
vc.view.backgroundColor = .red
navigationController?.pushViewController(vc, animated: true)
您必须创建另一个包含 UI 的视图控制器。
class SecondViewController: UIViewController {
private let textView = UITextView()
func viewDidLoad() {
super.viewDidLoad()
// Add text view with auto layout
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
textView.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
}
然后在尝试推送新的视图控制器时使用此 SecondViewController
let vc = SecondViewController()
vc.view.backgroundColor = .red
navigationController?.pushViewController(vc, animated: true)
您可以使用指定的标识符声明视图控制器
let vc = UIStoryboard.init(name: "Authentication", bundle: Bundle.main).instantiateViewController(withIdentifier: "Login") as? LoginVC
self.navigationController?.pushViewController(vc!, animated: true)