swift 函数响应式方法不执行
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DisposeBag
要放在外边哦,绑定会在它的deinit
里被取消。试试这个,大家都在用,https://github.com/RxSwiftCom...。