RxSwift 只触发一次
RxSwift only trigger once
searchController.searchBar.rx.text
.asDriver()
.map{ [=10=] == "" || [=10=] == nil }
.drive(onNext: { (empty) in
if empty {
print("Search empty")
} else {
print("!!!!!!!!!!!!! empty")
}
}).disposed(by: disposeBag)
第一次启动时会调用一次,当我输入第一个字符串时调用第二次,但不再有效,错误在哪里?谢谢!!!
我检查了你的代码,发现设置 UISearchBar
的委托使驱动程序只触发一次。例如:
self.searchController.searchBar.delegate = self
如果未设置委托 - 驱动程序将按您的预期触发。所以你应该做一些搜索委托,看看它是否能帮助你解决问题。
这是一个代表此行为的示例项目:
import Foundation
import UIKit
import RxCocoa
import RxSwift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? = UIWindow(frame: UIScreen.main.bounds)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window?.rootViewController = UINavigationController(rootViewController: Controller())
window?.makeKeyAndVisible()
return true
}
}
final class Controller: UIViewController, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate {
lazy var disposeBag = DisposeBag()
lazy var searchController = UISearchController(searchResultsController: nil)
init() {
super.init(nibName: nil, bundle: nil)
self.view.backgroundColor = UIColor.orange
self.searchController.searchResultsUpdater = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.navigationItem.titleView = self.searchController.searchBar
self.definesPresentationContext = true
self.searchController.delegate = self
self.searchController.dimsBackgroundDuringPresentation = false
// self.searchController.searchBar.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchBar.rx.text
.asDriver()
.map{ [=11=] == "" || [=11=] == nil }
.drive(onNext: { (empty) in
if empty {
print("Search empty")
} else {
print("!!!!!!!!!!!!! empty")
}
})
.disposed(by: disposeBag)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateSearchResults(for searchController: UISearchController) {
//
}
}
searchController.searchBar.rx.text
.asDriver()
.map{ [=10=] == "" || [=10=] == nil }
.drive(onNext: { (empty) in
if empty {
print("Search empty")
} else {
print("!!!!!!!!!!!!! empty")
}
}).disposed(by: disposeBag)
第一次启动时会调用一次,当我输入第一个字符串时调用第二次,但不再有效,错误在哪里?谢谢!!!
我检查了你的代码,发现设置 UISearchBar
的委托使驱动程序只触发一次。例如:
self.searchController.searchBar.delegate = self
如果未设置委托 - 驱动程序将按您的预期触发。所以你应该做一些搜索委托,看看它是否能帮助你解决问题。
这是一个代表此行为的示例项目:
import Foundation
import UIKit
import RxCocoa
import RxSwift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? = UIWindow(frame: UIScreen.main.bounds)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window?.rootViewController = UINavigationController(rootViewController: Controller())
window?.makeKeyAndVisible()
return true
}
}
final class Controller: UIViewController, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate {
lazy var disposeBag = DisposeBag()
lazy var searchController = UISearchController(searchResultsController: nil)
init() {
super.init(nibName: nil, bundle: nil)
self.view.backgroundColor = UIColor.orange
self.searchController.searchResultsUpdater = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.navigationItem.titleView = self.searchController.searchBar
self.definesPresentationContext = true
self.searchController.delegate = self
self.searchController.dimsBackgroundDuringPresentation = false
// self.searchController.searchBar.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchBar.rx.text
.asDriver()
.map{ [=11=] == "" || [=11=] == nil }
.drive(onNext: { (empty) in
if empty {
print("Search empty")
} else {
print("!!!!!!!!!!!!! empty")
}
})
.disposed(by: disposeBag)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateSearchResults(for searchController: UISearchController) {
//
}
}