Access Container View Controller from Parent iOS

后端 未结 11 2130
遥遥无期
遥遥无期 2020-11-28 18:13

in iOS6 I noticed the new Container View but am not quite sure how to access it\'s controller from the containing view.

Scenario:

相关标签:
11条回答
  • 2020-11-28 18:16

    In case someone is looking for Swift 3.0,

    viewController1, viewController2 and so on will then be accessible.

    let viewController1 : OneViewController!
    let viewController2 : TwoViewController!
    
    // Safety handling of optional String
    if let identifier: String = segue.identifier {
    
        switch identifier {
    
        case "segueName1":
            viewController1 = segue.destination as! OneViewController
            break
    
        case "segueName2":
            viewController2 = segue.destination as! TwoViewController
            break
    
        // ... More cases can be inserted here ...
    
        default:
            // A new segue is added in the storyboard but not yet including in this switch
            print("A case missing for segue identifier: \(identifier)")
            break
        }
    
    } else {
        // Either the segue or the identifier is inaccessible 
        print("WARNING: identifier in segue is not accessible")
    }
    
    0 讨论(0)
  • 2020-11-28 18:19

    There is another way using Swift's switch statement on the type of the view controller :

    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
      switch segue.destination
      {
        case let aViewController as AViewController:
          self.aViewController = aViewController
        case let bViewController as BViewController:
          self.bViewController = bViewController
        default:
          return
      }
    }
    
    0 讨论(0)
  • 2020-11-28 18:22

    With generic you can do some sweet things. Here is an extension to Array:

    extension Array {
        func firstMatchingType<Type>() -> Type? {
            return first(where: { $0 is Type }) as? Type
        }
    }
    

    You can then do this in your viewController:

    var viewControllerInContainer: YourViewControllerClass? {
        return childViewControllers.firstMatchingType()!
    }
    
    0 讨论(0)
  • 2020-11-28 18:23

    Yes, you can use the segue to get access the child view controller (and its view and subviews). Give the segue an identifier (such as alertview_embed), using the Attributes inspector in Storyboard. Then have the parent view controller (the one housing the container view) implement a method like this:

    - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
       NSString * segueName = segue.identifier;
       if ([segueName isEqualToString: @"alertview_embed"]) {
           AlertViewController * childViewController = (AlertViewController *) [segue destinationViewController];
           AlertView * alertView = childViewController.view;
           // do something with the AlertView's subviews here...
       }
    }
    
    0 讨论(0)
  • 2020-11-28 18:23

    for Swift Programming

    you can write like this

    var containerViewController: ExampleViewController?
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // you can set this name in 'segue.embed' in storyboard
        if segue.identifier == "checkinPopupIdentifierInStoryBoard" {
            let connectContainerViewController = segue.destinationViewController as ExampleViewController
            containerViewController = connectContainerViewController
        }
    }
    
    0 讨论(0)
  • 2020-11-28 18:28

    self.childViewControllers is more relevant when you need control from the parent. For instance, if the child controller is a table view and you want to reload it forcefully or change a property via a button tap or any other event on Parent View Controller, you can do it by accessing ChildViewController's instance and not via prepareForSegue. Both have their applications in different ways.

    0 讨论(0)
提交回复
热议问题