Swift:如何在导航控制器之后访问第二视图?
我对swift真的很陌生,目前正在为一个琐事应用程序做一个项目。现在我有了它,在按下开始按钮后,它会将当前视图控制器推到一边,并向用户显示一个新的视图(vc),但我不确定如何/在何处向用户在按下开始按钮后看到的新视图(vc)添加文本字段和按钮Swift:如何在导航控制器之后访问第二视图?,swift,navigationcontroller,tabbed,Swift,Navigationcontroller,Tabbed,我对swift真的很陌生,目前正在为一个琐事应用程序做一个项目。现在我有了它,在按下开始按钮后,它会将当前视图控制器推到一边,并向用户显示一个新的视图(vc),但我不确定如何/在何处向用户在按下开始按钮后看到的新视图(vc)添加文本字段和按钮 let vc = UIViewController() vc.view.backgroundColor = .red navigationController?.pushViewC
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)