Advertisement
Don_Mag

Untitled

Mar 19th, 2023 (edited)
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.64 KB | None | 0 0
  1. // set this as the Root viewController of a navigation controller
  2.  
  3. class NavTestVC: UIViewController {
  4.     var navID: Int = 0
  5.    
  6.     @objc func btnTap(_ sender: Any?) {
  7.         let vc = NavTestVC()
  8.         vc.navID = self.navID + 1
  9.         navigationController?.pushViewController(vc, animated: true)
  10.     }
  11.  
  12.     override func viewDidLoad() {
  13.         super.viewDidLoad()
  14.        
  15.         view.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
  16.        
  17.         var config = UIButton.Configuration.filled()
  18.         config.buttonSize = .medium
  19.         config.cornerStyle = .medium
  20.         config.title = "Push"
  21.        
  22.         let btnA = UIButton(configuration: config)
  23.         btnA.addAction (
  24.             UIAction { _ in
  25.                 self.btnTap(nil)
  26.             }, for: .touchUpInside
  27.         )
  28.  
  29.         let label = UILabel()
  30.        
  31.         label.translatesAutoresizingMaskIntoConstraints = false
  32.         view.addSubview(label)
  33.         btnA.translatesAutoresizingMaskIntoConstraints = false
  34.         view.addSubview(btnA)
  35.  
  36.         let g = view.safeAreaLayoutGuide
  37.         NSLayoutConstraint.activate([
  38.             label.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
  39.             label.centerXAnchor.constraint(equalTo: g.centerXAnchor),
  40.             btnA.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 40.0),
  41.             btnA.centerXAnchor.constraint(equalTo: g.centerXAnchor),
  42.         ])
  43.        
  44.         label.text = "This navID: \(navID)"
  45.     }
  46.    
  47.     override func viewWillAppear(_ animated: Bool) {
  48.         super.viewWillAppear(animated)
  49.         print("navID:", navID, #function)
  50.     }
  51.     override func viewDidAppear(_ animated: Bool) {
  52.         super.viewDidAppear(animated)
  53.         print("navID:", navID, #function)
  54.     }
  55.     override func viewWillDisappear(_ animated: Bool) {
  56.         super.viewWillDisappear(animated)
  57.         print("navID:", navID, #function)
  58.     }
  59.    
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement