Responder chain error - ios

I want to translate this kind of Objective-C code to Swift:
- (UINavigationController *)targetNaviCtrl{
if(!_targetNaviCtrl){
UIResponder *target = self.nextResponder;
do {
target = target.nextResponder;
} while (![target isKindOfClass:UINavigationController.self] && target != nil);
_targetNaviCtrl = (UINavigationController *)target;
}
return _targetNaviCtrl;
}
// by iterate its next responder, so I can get access the target viewController to do something.
// This is often used across several hierarchies.
// A root B, B push C , C push D. I use A in D viewController.
I met some trouble.
code repo
Apple Doc: next
Declaration
var next: UIResponder? { get }
Return Value
The next object in the responder chain or nil if this is the last object in the chain.
I want to access the left tarBarController in the right viewController.
class ViewControllerEightLayer
// the right viewController.
class ViewControllerEightLayer: UIViewController {
override var next: UIResponder?{
get{
return super.next
}
}// the right viewController.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
var nextResponder = self.next! // Here I call it
}
Here I call it, here is error info:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
class LayerTableVC
// the center a little right UITableViewController.
class LayerTableVC: UITableViewController {
override var next: UIResponder?{
get{
return super.next
}
}// the center a little right UITableViewController.
class LayerNavigationVC
// the center a little left UINavigationController.
class LayerNavigationVC: UINavigationController {
override var next: UIResponder?{
get{
return super.next
}
}// the center a little left UINavigationController.
class MainTabBarVC
// the left tarBarController
class MainTabBarVC: UINavigationController {
override var next: UIResponder?{
get{
return self
}
}// the left tarBarController
I do not know how to solve it in this way. Any suggestions?
Maybe It helps,
self.next?.target(forAction: <#T##Selector#>, withSender: <#T##Any?#>)
It seems wired.
I did it by the code ,
let tabBarViewController = UIApplication.shared.keyWindow!.rootViewController as! UITabBarController
And I want to know the responder chain solution
PS:
aim of digging through the responder chain is curiosity.
It happened , I do not think it will happen .
So I wonder why.
I'd like to reverse-engineering.
PPS:
My intend is to control the tool bar to hide and show. Not providing data from to
extension ViewControllerEightLayer: UITextFieldDelegate{
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.text == "1" {
tabBarViewController.tabBar.isHidden = false
}
else{
tabBarViewController.tabBar.isHidden = true
}
return true
}
This is a personal experiment project, not of my corp.

You seem to be asking for how to walk up the responder chain. Here's how.
func showResponderChain(_ r: UIResponder) {
var r : UIResponder! = r
repeat { print(r, "\n"); r = r.next } while r != nil
}

Related

MVVM + RXSwift+ Coordinator how to set data?

Good night!
Can you tell me how can I write data from controller 2 to controller 1?
I have a coordinate at the main screen.
final class MenuCoffeLikeCoordinator: TabBarPresentableCoordinator {
var tabBarItem: UITabBarItem = {
let title = "Меню"
let image = UIImage(asset: Resources.Assets.TabBarItems.mainTabBar)
let selectedImage = UIImage(asset: Resources.Assets.TabBarItems.mainTabBarSelected)
let item = UITabBarItem(title: title, image: image, selectedImage: selectedImage)
return item
}()
var navigationController: UINavigationController
init(navigationController: UINavigationController = UINavigationController()) {
self.navigationController = navigationController
}
var didFinish: (() -> Void)?
func start() {
self.navigationController.pushViewController(createMenuCoffeLikeFlow(), animated: true)
}
func stop() {}
func createMenuCoffeLikeFlow() { -> UIViewController {
let menuController = MenuCoffeLikeAssembler.createModule()
menuController.rx.didTapMapLayer.onNext {
let controller = self.createCoffeeBarMap()
self.navigationController.pushViewController(controller, animated: true)
}
return menuController
}
private func createCoffeeBarMap() -> UIViewController {
let controller = CoffeeBarContainerAssembler.createModule()
controller.obsRelay.subscribe(onNext: { event in
self.navigationController.popViewController(animated: true)
})
return controller
}
}
In the createMenuCoffeLikeFlow function, I create the main screen, and when I click on the button, I go to screen 2 (createCoffeeBarMap)
Inside the function (createCoffeeBarMap), I subscribe to the PublishSubject, and when the data changes, I get a new text.
I need to write this text in the menuCoffeeControler which is in the createMenuCoffeLikeFlow function. How can i do this?
Here's how I would implement it using my Cause Logic Effect (CLE) architecture. With CLE you don't need to implement a Coordinator because a reusable Coordinator class already exists in the library. This means less code for you to write.
Unlike yours, this sample is complete and will compile. The only thing missing is the creation and layout of the views inside the view controllers.
import Cause_Logic_Effect
import RxCocoa
import RxSwift
import UIKit
/// This function produces the view controller that is attached to your tab bar controller. I don't put the
/// `UITabBarItem` in here. Instead I attach that when connecting to the tab bar controller.
func menuCoffeLikeTab() -> UIViewController {
// the `configure` function calls its closure inside the viewDidLoad method.
let menuController = MenuController().configure { $0.connect() }
let controller = UINavigationController(rootViewController: menuController)
return controller
}
/// It depends on how you want to layout your view controllers on whether anything else goes in here. If you
/// use storyboards, then add `#IBOutlet` before the views here. If you create your views programatically
/// then add a `loadView()` override.
final class MenuController: UIViewController {
var mapLayerButton: UIButton!
var textField: UITextField!
let disposeBag = DisposeBag()
}
extension MenuController {
func connect() {
// This is the meat. The `coffeeBarResponse` observable pushes the
// CoffeeBarController onto the navigation stack when approprate and
// then emits any values produced by it. Notice how this looks alot like
// a network call except you are querying the user instead of the server.
let coffeeBarResponse = mapLayerButton.rx.tap
.flatMapFirst(pushScene(on: navigationController!, animated: true) {
CoffeeBarController().scene { $0.connect() }
})
.share()
// The pushScene function above will create a coordinator for the
// CoffeeBarController. When needed, the coordinator will create the
// view controller, call its `connect` and emit any values from that.
// When the Observable completes, the coordinator will pop the view
// controller off.
coffeeBarResponse
.bind(to: textField.rx.text)
.disposed(by: disposeBag)
}
}
final class CoffeeBarController: UIViewController {
var saveButton: UIButton!
var textField: UITextField!
}
extension CoffeeBarController {
func connect() -> Observable<String> {
// when the user taps the save button, this will emit whatever value is
// in the text field and then complete the observable.
saveButton.rx.tap
.withLatestFrom(textField.rx.text.orEmpty)
.take(1)
}
}
Like I said above, this uses a reusable Coordinator class that is part of the library instead of you having to write your own all the time. This architecture will significantly reduce the amount of boilerplate code you have to write. Learn more at https://github.com/danielt1263/CLE-Architecture-Tools and join the RxSwift Slack to learn more about RxSwift in general.
this is a typical scenario where DI comes to rescue. You have to have some kind of a shared container which will register and resolve dependencies. I use Dip https://github.com/AliSoftware/Dip.git and here is an example with your code. The idea is the following - you register closure in one VC and pass it to another.

How to change a tab bar item from the tab bar controller

I got tab bar controller with three view controllers setup. One of those view controllers changes its tab bar item badgeValue when I open it. I would like to change this badgeValue already when I arrive at the first tab.
I created a Tabbarcontoller: UITabBarController class but don't know how to easily access the items of the sub views. Here is the code from my Tabbarcontoller class:
class TabBarController: UITabBarController, MainMethodsDelegate {
var myFriendsRequests: [UserInfo] = []
var friendRequestsCount: Int = 0
func getFriendsRequests_Methods_Destination(myFriendsRequest: UserInfo) {
myFriendsRequests.append(myFriendsRequest)
self.friendRequestsCount = myFriendsRequests.count
DispatchQueue.main.async {
//friendsBarItem.badgeValue = String(self.friendRequestsCount)
}
}
let mainMethods = MainMethods()
override func viewDidLoad() {
super.viewDidLoad()
mainMethods.delegate = self
mainMethods.getFriendsRequests()
}
}
And here the working code from the sub view controller:
class FriendsViewController: UIViewController, MainMethodsDelegate {
var myFriendsRequests: [UserInfo] = []
var friendRequestsCount: Int = 0
func getFriendsRequests_Methods_Destination(myFriendsRequest: UserInfo) {
myFriendsRequests.append(myFriendsRequest)
self.friendRequestsCount = myFriendsRequests.count
DispatchQueue.main.async {
self.friendsBarItem.badgeValue = String(self.friendRequestsCount)
}
}
let mainMethods = MainMethods()
override func viewDidLoad() {
super.viewDidLoad()
mainMethods.delegate = self
mainMethods.getFriendsRequests()
}
#IBOutlet weak var friendsBarItem: UITabBarItem!
}
I'm looking for a simple way to access the sub view controllers or at least the bar items where it says:
//friendsBarItem.badgeValue = String(self.friendRequestsCount)
I'm not sure if delegates is the right way to go?
I would leave the bar item out of the friends controller. It does not need to know about the tabBar. If I were you, I would create a protocol to let know your tabBarController that the number of friend requests has changed.
First, define the protocol:
protocol FriendsRequestDelegate {
func friendsRequestsDidChange(number: Int)
}
Then, add the variable to your FriendsViewController:
weak var delegate: FriendsRequestDelegate?
And, we need to trigger that func, still in FriendsViewController, after fetching the number of friends request, add;
delegate?.friendsRequestsDidChange(number: myFriendsRequests.count)
Finally, make your TabBarController conform to this protocol;
extension TabBarController: FriendsRequestDelegate {
func friendsRequestsDidChange(number: Int) {
friendsBarItem.badgeValue = String(number)
}
}
See what I mean? This way your friendsRequestController doesn't know about the tabBar and it'll keep your code clean.
I've found a quite simple way to access the barItems. I only had to access the array of tabBar items with:
tabBar.items![2].badgeValue = ""
Here is my code for the UITabBarController:
class TabBarController: UITabBarController, MainMethodsDelegate {
var myFriendsRequests: [UserInfo] = []
var friendRequestsCount: Int = 0
func getFriendsRequests_Methods_Destination(myFriendsRequest: UserInfo) {
myFriendsRequests.append(myFriendsRequest)
self.friendRequestsCount = myFriendsRequests.count
DispatchQueue.main.async {
self.tabBar.items![2].badgeValue = String(self.friendRequestsCount)
}
}
let mainMethods = MainMethods()
override func viewDidLoad() {
super.viewDidLoad()
mainMethods.delegate = self
mainMethods.getFriendsRequests()
}
}
The sub view controller does not need to check and update the badgeValue anymore. (This is fine in my case since the getFriendsRequests_Methods_Destination(myFriendsRequest: UserInfo) method gets triggered as soon as there is a new friends request anyways using a (Firestore) snapshot listener.)
My initial way was to access the tabItem from each of the sub view controllers individually using tabBarController!.tabBar.items![2].badgeValue = "" But this would have been redundant in my case.

Keyboard is appearing and immediately disappearing when UISearchBar.becomeFirstResponder() is getting called

I have UISearchController in the navigationItem.searchController and I want to make it focus when the user selects "Search" from the menu.
So shortly, when the user is tapping on the "Search" option in the menu (UITableViewCell) it's getting the view controller that have the searchController in it and calling:
guard let navigationVC = presentingViewController as? UINavigationController else { return }
guard let documentsVC = navigationVC.topViewController as? DocumentsViewController else { return }
documentsVC.searchController.searchBar.becomeFirstResponder()
Then, the UISearchBar is getting focus, the keyboard is appearing and then it's immediately disappearing, and I don't have any code that would make it disappear (like view.endEditing()).
1 GIF is worth more than 1,000 words:
So, after many tries I got some way to make it work, but I'm sure there is a much more elegant ways to do this, so if someone think that they have better way, please post it here and I may use it and mark your answer as the correct one.
Create the function focusOnSearchBar() in YourViewController:
func focusOnSearchBar() {
let searchBar = searchController.searchBar
if searchBar.canBecomeFirstResponder {
DispatchQueue.main.async {
searchBar.becomeFirstResponder()
}
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.focusOnSearchBar()
}
}
}
What it actually do is use itself recursively and check (every 0.1 sec) if searchBar.canBecomeFirstResponder. This is the problematic/not elegant thing.
Then, add this to viewDidAppear():
if focusOnSearch {
searchController.isActive = true
}
Don't forget to add extension to your ViewController for UISearchControllerDelegate (and of course, set searchController.delegate = self) and implement didPresentSearchController (that will be invoke by setting searchController.isActive = true):
extension YourViewController: UISearchControllerDelegate {
func didPresentSearchController(_ searchController: UISearchController) {
if focusOnSearch {
focusOnSearchBar()
}
}
}
Now all you have to do is to set focusOnSearch = true in the prepare(for segue:sender:).
*Note: if you want to focusOnSearchBar while you are in the same viewController of the searchBar, just set:
focusOnSearch = true
searchController.isActive = true
And it will work by itself.
Make your searchbar first responder in the viewDidLoad method. That will make sure everything is ready before focusing the search bar.

VoiceOver Z gesture won't trigger when UIAlertController is active

I'm trying to use the Z gesture to dismiss a UIAlertController. I have a very simple app. It has a single view with 1 button. Tapping the button presents an alert. I have implemented
- (BOOL)accessibilityPerformEscape {
NSLog(#"Z gesture");
return YES;
}
With VoiceOver on, scrubbing the screen prints out "Z gesture," but when I press the button and the alert is visible, scrubbing the screen does nothing, the method is not called and nothing is printed. What do I have to do to get this to function while the alert is on screen?
Thanks...
To get the desired result on your alert view thanks to the scrub gesture, override accessibilityPerformEscape() in the alert view itself.
A solution could be to implement this override in an UIView extension as follows :
extension UIView {
override open func accessibilityPerformEscape() -> Bool {
if let myViewController = self.findMyViewController() as? UIAlertController {
myViewController.dismiss(animated: true,
completion: nil)
}
return true
}
private func findMyViewController() -> UIViewController? {
if let nextResponder = self.next as? UIViewController {
return nextResponder
} else if let nextResponder = self.next as? UIView {
return nextResponder.findMyViewController()
} else {
return nil
}
}
}
The code is short enough to be understood without further explanation. If it's not clear, don't hesitate to ask.
The function findMyViewController has been found here.

How to identify the viewController a button has been added onto?

I want to know what was the name of the viewController the the button was tapped. I've already looked into the answer provided here. But I believe won't work if I have different containerViews on the screen...where the viewController for each button may be different. Hence I need a different solution.
So I wrote this function to recursively look until it finds a UIViewController.
extension UIView{
func getTypeOfMyViewController() -> UIViewController.Type?{
if let _super = superview as? UIViewController{ // logically wrong!
return type(of:_super)
}else if let _super = superview{
return _super.getTypeOfMyViewController()
}else{
assertionFailure("everything should end up being on some viewController")
return nil
}
}
}
The only problem is this line:
if let _super = superview as? UIViewController{
It gives me the following warning:
Cast from 'UIView?' to unrelated type 'UIViewController' always fails
superview is a UIView and I don't know how to extract the 'viewController' which contains the 'view'.
Question1: So How can I do that?
Additionally I would like to use the getTypeOfMyViewController function as such:
extension UIButton{
open override var accessibilityLabel: String?{
get {
return "\(getTypeOfMyViewController.self): \(titleLabel?.text ?? "Null")"
}
set{
// nothing particular
}
}
}
I'm doing this because I want to create a unique identifier for all button taps in my logging system.
Question2: Does Swift offer any easier solution to this?
A view controller is not a view, so it can never be a superview. You have the right idea, but you're looking at the wrong hierarchy. What you want is not the view hierarchy but the responder chain.
Walk up the responder chain until you come to the view controller:
var r : UIResponder = theButton
repeat { r = r.next! } while !(r is UIViewController)
let vc = r as! UIViewController

Resources