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按钮,关闭模态视图")
})
}
}
网友评论