当我使用编程 UI 时,Perform Segue 不起作用
Perform Segue is not working when i am using programmatic UI
我正在使用程序化UI 和故事板为 UI 部分为我的个人项目创建一个应用程序,但是当我尝试从我的“SecondViewController”到我的“ ThirdViewController”,我像往常一样在我的 segue 中添加了“标识符”:
然后我从我的 SecondViewController 中调用了“performSegue”:
import UIKit
class SecondViewController: UIViewController {
private var myItem = [SecondItem]()
lazy var myTableView : UITableView = {
let myTable = UITableView()
myTable.translatesAutoresizingMaskIntoConstraints = false
return myTable
}()
private let myContentView : UIView = {
let view = UIView()
view.backgroundColor = .gray
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
lazy var label : UILabel = {
let myLabel = UILabel()
myLabel.text = "Hello"
return myLabel
}()
private let unameTextField : UITextField = {
let txtField = UITextField()
txtField.backgroundColor = .white
txtField.placeholder = "Username"
txtField.borderStyle = .roundedRect
txtField.translatesAutoresizingMaskIntoConstraints = false
return txtField
}()
private let pwordTxtField : UITextField = {
let txtField = UITextField()
txtField.placeholder = "Password"
txtField.borderStyle = .roundedRect
txtField.translatesAutoresizingMaskIntoConstraints = false
return txtField
}()
private let loginBtn : UIButton = {
let btn = UIButton(type: .system)
btn.backgroundColor = .blue
btn.setTitle("Login", for: .normal)
btn.tintColor = .white
btn.layer.cornerRadius = 5
btn.clipsToBounds = true
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
return btn
}()
//I called the "performSegue" here
@objc func btnPressed() {
performSegue(withIdentifier: "gotoBla", sender: self)
print("button pressed")
}
lazy var imageView : UIImageView = {
let image = UIImage(named: "image_4")
let imageView = UIImageView(image: image)
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
func setAutoLayout(){
let guide = view.safeAreaLayoutGuide
myContentView.anchor(top: guide.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: view.frame.height / 3, enableInsets: true)
imageView.anchor(top: myContentView.topAnchor, left: nil , bottom: nil , right: nil , paddingTop: 10, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 80, height: 80, enableInsets: true)
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
unameTextField.anchor(top: imageView.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor, paddingTop: 10, paddingLeft: 20, paddingBottom: 5, paddingRight: 20, width: 0, height: 40, enableInsets: true)
pwordTxtField.anchor(top: unameTextField.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor, paddingTop: 30, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40, enableInsets: true)
loginBtn.anchor(top: pwordTxtField.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor , paddingTop: 20, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40, enableInsets: true)
//TableView
myTableView.topAnchor.constraint(equalTo: myContentView.bottomAnchor).isActive = true
myTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
myTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
myTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
override func viewDidLoad() {
super.viewDidLoad()
myItem.append(SecondItem(text: "first"))
myItem.append(SecondItem(text: "Second"))
myItem.append(SecondItem(text: "Third"))
view.backgroundColor = .white
view.addSubview(myContentView)
myContentView.addSubview(unameTextField)
myContentView.addSubview(pwordTxtField)
myContentView.addSubview(loginBtn)
myContentView.addSubview(imageView)
myTableView.register(SecondTableViewCell.self, forCellReuseIdentifier: K.SecondTableViewCell.identifier)
myTableView.delegate = self
myTableView.dataSource = self
view.addSubview(myTableView)
setAutoLayout()
}
}
extension SecondViewController : UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
}
}
extension SecondViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
myItem.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.SecondTableViewCell.identifier, for: indexPath) as! SecondTableViewCell
cell.second = myItem[indexPath.row]
cell.selectionStyle = .none
return cell
}
}
对于第三个视图控制器,我还没有在其中添加一些代码
import UIKit
class ThirdViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
但每次我 运行 应用程序并单击登录按钮时,它总是给我这个错误:
这是我的应用程序的样子:
我在这里错过了什么吗?顺便说一句,我是初学者,希望大家能帮助我。谢谢
在这里发帖提问时,您最好花几分钟时间复习一下How to Ask
但是,根据您提供的少量信息...
几乎可以肯定的是,问题是您在 Storyboard 中添加了 View Controller,然后尝试通过代码不正确地使用它们。
例如,我猜您的“第一个”视图控制器中有代码可以像这样加载和显示 SecondViewController
:
@objc func showSecondTapped(_ sender: Any) {
let vc = SecondViewController()
navigationController?.pushViewController(vc, animated: true)
}
然后在 SecondViewController
中,您尝试使用 故事板关联的 segue 与此:
@objc func btnPressed(_ sender: Any) {
performSegue(withIdentifier: "gotoBla", sender: self)
print("button pressed")
}
但是,该 segue 不作为 SecondViewController
code 的一部分存在...它是 Storyboard 对象的一部分。
回到您的第一个视图控制器,如果您像这样加载并推送到 SecondViewController
:
@objc func showSecondTapped(_ sender: Any) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
navigationController?.pushViewController(vc, animated: true)
}
}
您随后可以调用 performSegue
,因为您是从情节提要中加载它的。
if let vc = storyboard?.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
navigationController?.pushViewController(vc, animated: true)
// if navigationController?.pushViewController doesn't work you can try this
present(vc, animated: true) {
// anything you want to perform after presenting new screen
}
// and try this to show in full screen with different transition effect
vc.modalTransitionStyle = .crossDissolve
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true) {
// anything you want to perform after presenting new screen
}
}
我正在使用程序化UI 和故事板为 UI 部分为我的个人项目创建一个应用程序,但是当我尝试从我的“SecondViewController”到我的“ ThirdViewController”,我像往常一样在我的 segue 中添加了“标识符”:
然后我从我的 SecondViewController 中调用了“performSegue”:
import UIKit
class SecondViewController: UIViewController {
private var myItem = [SecondItem]()
lazy var myTableView : UITableView = {
let myTable = UITableView()
myTable.translatesAutoresizingMaskIntoConstraints = false
return myTable
}()
private let myContentView : UIView = {
let view = UIView()
view.backgroundColor = .gray
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
lazy var label : UILabel = {
let myLabel = UILabel()
myLabel.text = "Hello"
return myLabel
}()
private let unameTextField : UITextField = {
let txtField = UITextField()
txtField.backgroundColor = .white
txtField.placeholder = "Username"
txtField.borderStyle = .roundedRect
txtField.translatesAutoresizingMaskIntoConstraints = false
return txtField
}()
private let pwordTxtField : UITextField = {
let txtField = UITextField()
txtField.placeholder = "Password"
txtField.borderStyle = .roundedRect
txtField.translatesAutoresizingMaskIntoConstraints = false
return txtField
}()
private let loginBtn : UIButton = {
let btn = UIButton(type: .system)
btn.backgroundColor = .blue
btn.setTitle("Login", for: .normal)
btn.tintColor = .white
btn.layer.cornerRadius = 5
btn.clipsToBounds = true
btn.translatesAutoresizingMaskIntoConstraints = false
btn.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
return btn
}()
//I called the "performSegue" here
@objc func btnPressed() {
performSegue(withIdentifier: "gotoBla", sender: self)
print("button pressed")
}
lazy var imageView : UIImageView = {
let image = UIImage(named: "image_4")
let imageView = UIImageView(image: image)
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
func setAutoLayout(){
let guide = view.safeAreaLayoutGuide
myContentView.anchor(top: guide.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: view.frame.height / 3, enableInsets: true)
imageView.anchor(top: myContentView.topAnchor, left: nil , bottom: nil , right: nil , paddingTop: 10, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 80, height: 80, enableInsets: true)
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
unameTextField.anchor(top: imageView.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor, paddingTop: 10, paddingLeft: 20, paddingBottom: 5, paddingRight: 20, width: 0, height: 40, enableInsets: true)
pwordTxtField.anchor(top: unameTextField.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor, paddingTop: 30, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40, enableInsets: true)
loginBtn.anchor(top: pwordTxtField.bottomAnchor, left: myContentView.leftAnchor, bottom: nil, right: myContentView.rightAnchor , paddingTop: 20, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40, enableInsets: true)
//TableView
myTableView.topAnchor.constraint(equalTo: myContentView.bottomAnchor).isActive = true
myTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
myTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
myTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
override func viewDidLoad() {
super.viewDidLoad()
myItem.append(SecondItem(text: "first"))
myItem.append(SecondItem(text: "Second"))
myItem.append(SecondItem(text: "Third"))
view.backgroundColor = .white
view.addSubview(myContentView)
myContentView.addSubview(unameTextField)
myContentView.addSubview(pwordTxtField)
myContentView.addSubview(loginBtn)
myContentView.addSubview(imageView)
myTableView.register(SecondTableViewCell.self, forCellReuseIdentifier: K.SecondTableViewCell.identifier)
myTableView.delegate = self
myTableView.dataSource = self
view.addSubview(myTableView)
setAutoLayout()
}
}
extension SecondViewController : UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
}
}
extension SecondViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
myItem.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.SecondTableViewCell.identifier, for: indexPath) as! SecondTableViewCell
cell.second = myItem[indexPath.row]
cell.selectionStyle = .none
return cell
}
}
对于第三个视图控制器,我还没有在其中添加一些代码
import UIKit
class ThirdViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
但每次我 运行 应用程序并单击登录按钮时,它总是给我这个错误:
这是我的应用程序的样子:
我在这里错过了什么吗?顺便说一句,我是初学者,希望大家能帮助我。谢谢
在这里发帖提问时,您最好花几分钟时间复习一下How to Ask
但是,根据您提供的少量信息...
几乎可以肯定的是,问题是您在 Storyboard 中添加了 View Controller,然后尝试通过代码不正确地使用它们。
例如,我猜您的“第一个”视图控制器中有代码可以像这样加载和显示 SecondViewController
:
@objc func showSecondTapped(_ sender: Any) {
let vc = SecondViewController()
navigationController?.pushViewController(vc, animated: true)
}
然后在 SecondViewController
中,您尝试使用 故事板关联的 segue 与此:
@objc func btnPressed(_ sender: Any) {
performSegue(withIdentifier: "gotoBla", sender: self)
print("button pressed")
}
但是,该 segue 不作为 SecondViewController
code 的一部分存在...它是 Storyboard 对象的一部分。
回到您的第一个视图控制器,如果您像这样加载并推送到 SecondViewController
:
@objc func showSecondTapped(_ sender: Any) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
navigationController?.pushViewController(vc, animated: true)
}
}
您随后可以调用 performSegue
,因为您是从情节提要中加载它的。
if let vc = storyboard?.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
navigationController?.pushViewController(vc, animated: true)
// if navigationController?.pushViewController doesn't work you can try this
present(vc, animated: true) {
// anything you want to perform after presenting new screen
}
// and try this to show in full screen with different transition effect
vc.modalTransitionStyle = .crossDissolve
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true) {
// anything you want to perform after presenting new screen
}
}