swift 函数响应式方法不执行

发布于 2022-09-11 21:38:28 字数 5277 浏览 13 评论 0

图片描述

import UIKit
import SnapKit
import SwiftExpand
import RxSwift
import RxCocoa

let minUsernameLength = 8;
let minPasswordLength = 6;

class BNUserLogInController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        view.addSubview(userNameTextField)
        view.addSubview(userNameValidLab)
        view.addSubview(userPwdTextField)
        view.addSubview(userPwdValidLab)
        view.addSubview(loginBtn)
        
        setupConfigure();
        
        view.getViewLayer()
    }
    
    func setupConfigure() -> Void {
        let disposeBag: DisposeBag = DisposeBag()
        
        userNameValidLab.text = "Username has to be at least \(minUsernameLength) characters"
        userPwdValidLab.text = "Password has to be at least \(minPasswordLength) characters"
        
        let usernameValid = userNameTextField.rx.text.orEmpty
            .map { $0.count >= minUsernameLength }
            .share(replay: 1)

        let passwordValid = userPwdTextField.rx.text.orEmpty
            .map { $0.count >= minPasswordLength }
            .share(replay: 1)
        
        let everythingValid = Observable.combineLatest(usernameValid, passwordValid) { $0 && $1 }
            .share(replay: 1)
        
        usernameValid
            .bind(to: userPwdTextField.rx.isEnabled)
            .disposed(by: disposeBag)
        
        usernameValid
            .bind(to: userNameValidLab.rx.isHidden)
            .disposed(by: disposeBag)
        
        passwordValid
            .bind(to: userPwdValidLab.rx.isHidden)
            .disposed(by: disposeBag)
        
        everythingValid
            .bind(to: loginBtn.rx.isEnabled)
            .disposed(by: disposeBag)
        
        loginBtn.rx.tap
            .subscribe(onNext: { [weak self] _ in DDLog(self!.loginBtn) })
            .disposed(by: disposeBag)
        
        //        loginBtn.rx.tap
        //            .subscribe(onNext: { [weak self] _ in
        //            DDLog("__登录__")
        //
        //            }, onError: { (error)->Void in
        //                DDLog("onError:\(error)")
        //
        //        }, onCompleted: { ()->Void in
        //            DDLog("onCompleted")
        //
        //        }, onDisposed: { ()->Void in
        //            DDLog("onDisposed")
        //
        //        })
        //        .disposed(by: disposeBag);
        //
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews();
        
        userNameTextField.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(64)
            make.left.equalToSuperview().offset(kX_GAP)
            make.right.equalToSuperview().offset(-kX_GAP)
            make.height.equalTo(35)
        }
        
        userNameValidLab.snp.makeConstraints { (make) in
            make.top.equalTo(userNameTextField.snp.bottom).offset(kPadding)
            make.left.equalToSuperview().offset(kX_GAP)
            make.right.equalToSuperview().offset(-kX_GAP)
            make.height.equalTo(20)
        }
        
        userPwdTextField.snp.makeConstraints { (make) in
            make.top.equalTo(userNameValidLab.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(kX_GAP)
            make.right.equalToSuperview().offset(-kX_GAP)
            make.height.equalTo(35)
        }
        
        userPwdValidLab.snp.makeConstraints { (make) in
            make.top.equalTo(userPwdTextField.snp.bottom).offset(kPadding)
            make.left.equalToSuperview().offset(kX_GAP)
            make.right.equalToSuperview().offset(-kX_GAP)
            make.height.equalTo(20)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(userPwdValidLab.snp.bottom).offset(40)
            make.left.equalToSuperview().offset(kX_GAP)
            make.right.equalToSuperview().offset(-kX_GAP)
            make.height.equalTo(45)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        
    }

    // MARK: -lazy
    lazy var userNameTextField: UITextField = {
        var view = UITextField();
        view.placeholder = "用户名:8~20字符"
        return view;
    }()
    
    lazy var userPwdTextField: UITextField = {
        var view = UITextField();
        view.placeholder = "用户密码:6~20字符(不能纯数字)"
        return view;
    }()
    
    lazy var userNameValidLab: UILabel = {
        var view = UILabel();
        view.textColor = .red
        return view;
    }()
    
    lazy var userPwdValidLab: UILabel = {
        var view = UILabel();
        view.textColor = .red

        return view;
    }()
    
    lazy var loginBtn: UIButton = {
//        var btn = UIButton(type: .custom);
        var view = UIView.createBtn(title: "登录", imgName: nil);
        view.setTitleColor( .white, for: .normal)
        view.setTitleColor( .black, for: .disabled)
        view.setTitle("用户名/密码错误", for: .disabled)
        view.backgroundColor = .theme
        return view;
    }()
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾兮 2022-09-18 21:38:28

DisposeBag 要放在外边哦,绑定会在它的 deinit 里被取消。

试试这个,大家都在用,https://github.com/RxSwiftCom...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文