简体   繁体   English

以编程方式返回上一页

[英]Going back to the previous page programatically

enter image description here I am making a user login page for my IOS App. 在此处输入图像描述,我正在为IOS应用创建用户登录页面。 I am trying to make everything programmatically without using a segue in the main story board. 我正在尝试以编程方式制作所有内容,而不在主故事板上使用segue。 I have managed to go to the next page as you can see in the code and create a custom left back bar item. 正如您在代码中看到的那样,我设法转到了下一页,并创建了一个自定义的左后栏项目。 But I don't know how to go back to the first page when I click on that button. 但是,当我单击该按钮时,我不知道如何返回首页。 I have attached the screen shot of my story board and app below. 我在下面附上了我的故事板和应用程序的屏幕截图。 Can you please tell me how I can go back to the first page by clicking the X button? 您能告诉我如何通过单击X按钮返回第一页吗? Here is my code: 这是我的代码:

import UIKit 导入UIKit

class FirstPage: UIViewController { FirstPage类:UIViewController {

let hgt = UIScreen.main.bounds.height/2


private let loginSignup: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Login or Sign Up", for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
    button.setTitleColor(.green, for: .normal)
    button.addTarget(self, action: #selector(loginSignupBtn), for: .touchUpInside)
    return button
}()

@objc private func loginSignupBtn() {
    let vc = self.storyboard!.instantiateViewController(withIdentifier: "Login") as! Login
    vc.loadViewIfNeeded()
    self.navigationController?.pushViewController(vc, animated: true)
    self.navigationController?.isNavigationBarHidden = false
    let backItem = UIBarButtonItem(title: "X ", style: UIBarButtonItem.Style.done, target: nil, action: nil)
    let font = UIFont.boldSystemFont(ofSize: 26)
    backItem.setTitleTextAttributes([NSAttributedString.Key.font:font] ,for: .normal)
    backItem.tintColor = UIColor.green
    vc.navigationItem.leftBarButtonItem = backItem
}
override func viewDidLoad() {
    super.viewDidLoad()
    setupLayout()
}
override func viewWillAppear(_ animated: Bool) {

}
private func setupLayout() {
    let topImageContainerView = UIView()
    //topImageContainerView.backgroundColor = .yellow
    view.addSubview(topImageContainerView)
    //enable auto layout
    topImageContainerView.translatesAutoresizingMaskIntoConstraints = false
    topImageContainerView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    topImageContainerView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    topImageContainerView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    topImageContainerView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5).isActive = true
    view.addSubview(loginSignup)
    loginSignup.translatesAutoresizingMaskIntoConstraints = false
    loginSignup.topAnchor.constraint(equalTo: view.topAnchor,constant:hgt/2).isActive = true
    loginSignup.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    loginSignup.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    loginSignup.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.5).isActive = true

}

} }

enter image description here 在此处输入图片说明

Here you need to add target and selector for when the x button is clicked 在这里,您需要在单击x按钮时添加目标和选择器

let backItem = UIBarButtonItem(title: "X ", 
  style: UIBarButtonItem.Style.done,
   target: self, action:#selector(goBack))

@objc func goBack(_ bar:UIBarButtonItem) {
   self.navigationController?.popToRootViewController(animated:true)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM