dengApro
dengApro

Reputation: 4008

Responder chain error

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.

dist

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.

Upvotes: 1

Views: 433

Answers (1)

matt
matt

Reputation: 535027

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
}

Upvotes: 2

Related Questions