swift 4.2程式碼實現模態檢視導航
阿新 • • 發佈:2020-12-31
AppDelegate.swift中application部分:
private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.backgroundColor = UIColor.white let navigationController = UINavigationController(rootViewController: ViewController()) self.window?.rootViewController = navigationController self.window?.makeKeyAndVisible() return true }
ViewController.swift:
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let screen = UIScreen.main.bounds self.navigationItem.title = "登入" /// 1.新增使用者ID:標籤 let labelId = UILabel(frame: CGRect(x: 80,y: 115, width: 80, height: 21)) labelId.text = "使用者ID:" self.view.addSubview(labelId) /// 2.新增使用者ID TextField let textFieldId = UITextField(frame: CGRect(x: 170, y: 106, width: 100, height: 30)) textFieldId.borderStyle = .roundedRect self.view.addSubview(textFieldId) /// 3.新增密碼:標籤 let labelPwd = UILabel(frame: CGRect(x: 80, y: 178, width: 80, height: 21)) labelPwd.text = "密碼:" self.view.addSubview(labelPwd) /// 4.新增密碼 TextField let textFieldPwd = UITextField(frame: CGRect(x: 170, y: 169, width: 100, height: 30)) textFieldPwd.borderStyle = .roundedRect self.view.addSubview(textFieldPwd) /// 5.新增 登入按鈕 let buttonLogin = UIButton(type: .system) buttonLogin.frame = CGRect(x: (screen.size.width - 30)/2, y: 231, width: 50, height: 30) buttonLogin.setTitle("登入", for: UIControl.State()) self.view.addSubview(buttonLogin) /// 6.新增 註冊按鈕 let buttonReg = UIButton(type: .system) buttonReg.frame = CGRect(x: (screen.size.width - 30)/2, y: 294, width: 50, height: 30) buttonReg.setTitle("註冊", for: UIControl.State()) buttonReg.addTarget(self, action: #selector(onClick(_:)), for: .touchUpInside) self.view.addSubview(buttonReg) NotificationCenter.default.addObserver(self, selector: #selector(registerCompletion(_:)), name: Notification.Name(rawValue: "RegisterCompletionNotification"), object: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() NotificationCenter.default.removeObserver(self) } @objc func onClick(_ sender: AnyObject){ let registerViewController = RegisterViewController() //用於例項化導航控制器UINavigationController, 將RegisterViewController嵌入到導航控制器中. let navigationController = UINavigationController(rootViewController: registerViewController) //呈現模態檢視 self.present(navigationController,animated: true,completion: nil) } @objc func registerCompletion(_ notionfication: Notification){ let theData = notionfication.userInfo! let username = theData["username"] as! String print("username = " , username) } }
RegisterViewController.swift:
import UIKit class RegisterViewController: UIViewController { var txtUsername: UITextField! override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = "註冊" let saveButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(save(_:))) let cancelButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancel(_:))) self.navigationItem.rightBarButtonItem = saveButtonItem self.navigationItem.leftBarButtonItem = cancelButtonItem /// 1.新增使用者ID:標籤 let labelId = UILabel(frame: CGRect(x: 35, y: 187, width: 100, height: 21)) labelId.text = "使用者ID:" self.view.addSubview(labelId) /// 2.新增Username TextField self.txtUsername = UITextField(frame: CGRect(x: 180, y: 178, width: 131, height: 30)) self.txtUsername.borderStyle = .roundedRect self.view.addSubview(self.txtUsername) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @objc func save(_ sender: AnyObject) { self.dismiss(animated: true) { () -> Void in NSLog("點選Save按鈕,關閉模態檢視") let dataDict = ["username" : self.txtUsername.text!] NotificationCenter.default.post(name: Notification.Name(rawValue: "RegisterCompletionNotification"), object: nil, userInfo: dataDict) } } @objc func cancel(_ sender: AnyObject) { self.dismiss(animated: true, completion: { NSLog("點選Cancel按鈕,關閉模態檢視") }) } }