Working with UIView subview's subview after addSubview

Discussion in 'iOS Programming' started by moonman239, Feb 3, 2017.

  1. moonman239 macrumors 68000

    Mar 27, 2009
    I have a view, which will call Alice. In turn, Alice has a subview named Bob. Bob, in turn, has a subview named Carol. Alice and Bob are designed in Interface Builder, while Carol is an Apple-framework view.

    Bob has a property to store Carol, and the storing takes place in Bob's addSubview:
    override func addSubview(_ view: UIView) {
            Carol = view as? UIImageView
    However, Alice must call Bob's doSomething function, wherein Bob must then set a property on Carol. Thus, the call takes place in Alice's layoutSubviews function. Here's Bob's doSomething message:

    func doSomething(theCase: CaseType) {
            self.isHidden = false
            switch theCase {
            case .case2:
                Carol.isHidden = false
            default: break
    Alice's layoutSubviews method looks something like this:
    override func layoutSubviews()
    However, this solution appears to be invalid because "Carol", the property, hasn't been set yet. Further, the view hierarchy I defined in Interface Builder is exactly what I want it to be.

    So my question is, where is the best place to put my call to doSomething()?
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    I'm unclear if your three views are all laid out in IB. If so then the properties to the views should be IBOutlets and the view hierarchy will be setup when awakeFromNib or viewDidLoad are called. Does doSomething need to be called one time only or more than one time?
  3. moonman239 thread starter macrumors 68000

    Mar 27, 2009
    They're all laid out in Interface Builder - same XIB.

    doSomething is called once for every Bob.
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    OK, then I think I answered the question. Make IBOutlets for these views and call the method in awakeFromNib(). You could also add a didSet{ } block for the bob IBOutlet and call doSomething() then, although at awakeFromNib() time you are guaranteed that the entire view hierarchy is set up.

    Does that work for you?
  5. bjet767 macrumors 6502a

    Oct 2, 2010
    I have a question, why are you still using .xib files when Xcode has this marvelous tool called "storyboard"?

    I see you're working in Swift and it's a whole lot easier to use a storyboard and let the main viewcontroller be the heart of the initial view. I also think storyboards and biewcontrollers are Apple's preferred method of view layouts.

    Just asking because on most other sites the examples are using storyboards and viewcontrollers and I gave up .xib files years ago.

  6. Mascots macrumors 68000


    Sep 5, 2009
    Nibs are great resources!

    A common use case is a table view cell used in many places - load in the Nib into several different table views instead of duplicating it in SB. Headers and footers can be loaded in as well.
  7. bjet767 macrumors 6502a

    Oct 2, 2010
    I thought about it for a moment and can see the use of subclassing the cell and using the visual layout of the xib file so one can reuse the same cell.

    Typically I just design the cell on the tableview and then subclass it from there with links to my data.

    I've gotten lazy, or more efficient, not sure which one is correct, and like to use as many automatic functions as possible. I'm getting old and the desire to custom code everything is waning.

    I'm working on a project right now that has grown from simple to rather huge and includes the Watch. My latest challenge is to get HR data real time from the HealthKit for those people who don't want to purchase the much more accurate HR chest strap. But I'm finding HealthKit does have some nice built in features that I might use.

    BTW my surprise though is in the original poster's multiple views and the use of .xib files. A trick I use, and found it does not add real extra memory overhead, is to stack views on each other in Storyboard and then make them visible as needed.

Share This Page

6 February 3, 2017