033_swift_自定义视图_使用间接代理传递消息

//
//  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!

    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.shadowOffset = CGSize(width: 0.0, height: 3.0)
        textField.layer.shadowOpacity=0.45
        textField.layer.shadowRadius=3
        textField.backgroundColor = .lightGray
        self.addSubview(textField)
        
    }
 
    func setController(controllor: ViewController){
        self.textField.delegate = controllor as! UITextFieldDelegate
    }
    
    
    
    
    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 UIKit

class 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.nameFiled.setController(controllor: self)
        self.view.addSubview(self.nameFiled)
        
        self.passwordField = CustomView(frame: CGRect(x: 20, y: 140, width: width, height: height))
          self.nameFiled.setController(controllor: self)
        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)
    }
    
    @objc func submitForm(_ sender:UIButton){
        print("submitForm(_:)")

    }
    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
        }
    }
    
    
}

猜你喜欢

转载自blog.csdn.net/mp624183768/article/details/105915807