1

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.

dengApro
  • 3,848
  • 2
  • 27
  • 41
  • "I call it, and i failed" Failed how? Found nil? – bauerMusic Apr 11 '18 at 17:29
  • I wonder if you're asking the wrong question... Is your intent to provide data from `View Controller Eight Layer` to `Layer Cases View Controller`? – DonMag Apr 11 '18 at 17:35
  • Having said that, @DonMag is right. What is the aim of digging through the responder chain? – bauerMusic Apr 11 '18 at 17:39
  • Updated, @Mr Music & DonMag – dengApro Apr 11 '18 at 18:12
  • Wait, why would you override `next` to call `super`???? – matt Apr 12 '18 at 02:46
  • I googled . https://stackoverflow.com/questions/40677049/nextresponder-in-swift-3 . And the senmatic is logical .@matt – dengApro Apr 12 '18 at 02:51
  • 1
    No, I don't get it. `next` already exists and does what it does, correctly. Overriding it is just a way to break stuff (which is what you seem to be doing). – matt Apr 12 '18 at 19:34
  • @matt , I build an other project to test it. The result is quite apple doc , not yours. Maybe something I missed . Here is the link https://github.com/dengV/responder_for_stack_over_flow . Do do hope you can help me to solve it. – dengApro Apr 13 '18 at 01:21
  • To test _what_? I still have not understood the problem at all. What exactly is the goal? – matt Apr 13 '18 at 01:55
  • I want to get the superior instance ( for example : the root view controller)in the presented view controller through responder chain. The Obj-C code of the start part works perfect. How to achieve it in swift? `self.next` , I aways got nil as apple doc. So I override it. How do you say 'next' already exists? @matt – dengApro Apr 13 '18 at 02:36
  • 1
    I say that `next` already exists because it does. https://developer.apple.com/documentation/uikit/uiresponder/1621099-next – matt Apr 13 '18 at 02:39

1 Answers1

2

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
}
matt
  • 515,959
  • 87
  • 875
  • 1,141