博客
关于我
034_swift_自定义视图_使用通知传递消息
阅读量:348 次
发布时间:2019-03-04

本文共 3507 字,大约阅读时间需要 11 分钟。

////  CustomView.swift//  DemoApp////  Created by liuan on 2020/5/3.//  Copyright © 2020 anguo.com. All rights reserved.//import UIKit//自定义文本视图class CustomView: UIView,UITextFieldDelegate {    var textField:UITextField!    weak var controllor : ViewController?    override init(frame:CGRect){        super.init(frame: frame)        textField=UITextField(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))        textField.font=UIFont.boldSystemFont(ofSize: 14)        textField.textColor = .purple        textField.layer.shadowColor=UIColor.black.cgColor        textField.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)        textField.layer.shadowOpacity=0.45        textField.layer.shadowRadius=3        textField.backgroundColor = .lightGray        textField.delegate=self        self.addSubview(textField)        }    func textFieldShouldReturn(_ textField: UITextField) -> Bool {        NotificationCenter.default.post(name: Notification.Name(rawValue: "checkFromNotification"),object: nil,userInfo: nil)        return true    }    required init?(coder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }    }
////  ViewController.swift//  DemoApp////  Created by liuan on 2020/4/23.//  Copyright © 2020 anguo.com. All rights reserved.//import UIKitclass ViewController: UIViewController,UITextFieldDelegate {    var nameFiled:CustomView!    var passwordField:CustomView!    var submitButton:UIButton!            var animationView:UIView!    override func viewDidLoad(){        super.viewDidLoad()                let width=Int(self.view.frame.size.width)-40        let height=40        self.nameFiled = CustomView(frame: CGRect(x: 20, y: 60, width: width, height: height))        self.view.addSubview(self.nameFiled)                self.passwordField = CustomView(frame: CGRect(x: 20, y: 140, width: width, height: height))        self.view.addSubview(self.passwordField)                self.submitButton = UIButton(frame: CGRect(x: 20, y: 240, width: width, height: height))        self.submitButton.setTitle("提交", for: .normal)        self.submitButton.backgroundColor = .gray        self.submitButton.isEnabled=false        self.submitButton.addTarget(self, action: #selector(ViewController.submitForm(_:)), for: .touchUpInside)        self.view.addSubview(self.submitButton)        NotificationCenter.default.addObserver(self, selector: #selector(checkForm(_:)), name: NSNotification.Name(rawValue: "checkFromNotification"), object: nil)    }        @objc func submitForm(_ sender:UIButton){        print("submitForm(_:)")    }    @objc func checkForm(_ notifiy:Notification?){        if self.nameFiled.textField.text != "" && passwordField.textField.text != ""{            self.submitButton.isEnabled = true            self.submitButton.backgroundColor = .orange        }else{            self.submitButton.isEnabled = false                self.submitButton.backgroundColor = .gray        }    }    func textFieldShouldReturn(_ textField: UITextField) -> Bool {        checkForm()        return true    }    func checkForm() {        if self.nameFiled.textField.text != "" && self.passwordField.textField.text != ""        {            self.submitButton.isEnabled=true            self.submitButton.backgroundColor = .orange        }else{            self.submitButton.isEnabled=false            self.submitButton.backgroundColor = .gray        }    }    deinit {        NotificationCenter.default.removeObserver(self)    }        }

 

转载地址:http://ltsr.baihongyu.com/

你可能感兴趣的文章
文件md5怎么会变化
查看>>
tablayout 滑动监听
查看>>
Error connecting to the service protoco
查看>>
windows 用户获取管理员权限
查看>>
Flutter 加载本地图片
查看>>
android带气泡的第三方选项卡
查看>>
好玩的editText
查看>>
自动安装服务2
查看>>
edittext弹出键盘或者隐藏键盘
查看>>
android用视频当做背景
查看>>
android 用action拦截打电话界面
查看>>
【已解决】Android Studio下,gradle project sync failed 错误
查看>>
打包命名
查看>>
实现谣传QQ中的手段——“1像素页面保活”
查看>>
Android UI效果篇-(3)用属性动画实现收缩菜单
查看>>
android中getLocationInWindow 和 getLocationOnScreen的区别
查看>>
Android反编译-揭秘猎豹设置默认浏览器逻辑
查看>>
Android onSaveInstanceState()和onRestoreInstanceState()调用时机
查看>>
错误: 编码GBK的不可映射字符
查看>>
python3 读写Excel
查看>>