1. 程式人生 > >RxSwift:rx.controllEvent初體驗

RxSwift:rx.controllEvent初體驗

輸入使用者名稱按回車,游標自動跳到密碼欄,輸入完密碼,鍵盤消失

//
//  ViewController.swift
//  RxSwiftTest
//
//  Created by travey on 2018/11/5.
//  Copyright © 2018年 ZhouShijie. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import SnapKit

    class ViewController: UIViewController {
        
        var textField1: UITextField!
        var textField2: UITextField!
        var label: UILabel!
        var btn: UIButton!
        let disposeBag = DisposeBag()

        override func viewDidLoad() {
            
            textField1 = UITextField()
            textField2 = UITextField()
            
            textField1.layer.borderWidth = 1
            textField2.layer.borderWidth = 1
            
            view.addSubview(textField1)
            view.addSubview(textField2)
            
            textField1.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(200)
                make.height.equalTo(30)
                make.width.equalTo(200)
            }
            
            textField2.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.top.equalTo(textField1.snp.bottom).offset(30)
                make.height.equalTo(30)
                make.width.equalTo(200)
            }
            
           textField1.rx.controlEvent(UIControlEvents.editingDidEndOnExit)
            .subscribe(onNext: { [weak self] _ in
                self?.textField2.becomeFirstResponder()
            })
            .disposed(by: disposeBag)
            
           textField2.rx.controlEvent(UIControlEvents.editingDidEndOnExit)
            .subscribe(onNext: { [weak self] _ in
                    self?.textField2.resignFirstResponder()
            })
            .disposed(by: disposeBag)
        }
    }