Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
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:
Code:
override func addSubview(_ view: UIView) {

        super.addSubview(view)

        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:

Code:
func doSomething(theCase: CaseType) {

        self.isHidden = false

        switch theCase {

       ...

        case .case2:

            Carol.isHidden = false

        default: break

        }

    }

Alice's layoutSubviews method looks something like this:
Code:
override func layoutSubviews()

    {

        super.layoutSubviews()
        Bob.doSomething()
    }

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()?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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?
 

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
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?
They're all laid out in Interface Builder - same XIB.

doSomething is called once for every Bob.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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?
 

bjet767

Suspended
Oct 2, 2010
967
320
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.

Thanks.
 

Mascots

macrumors 68000
Sep 5, 2009
1,667
1,417
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.
 

bjet767

Suspended
Oct 2, 2010
967
320
A common use case is a table view cell used in many places

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.