How to Grab The Parent Object from a Subview

Is there a way to grab the parent object from a subview?

You can get the superview by

if let button = textField.superview as? CustomButton {
// Do what you want to here
}

Accessing parent view of current view - iOS

If you're calling this directly from a UIView (and not a UIViewController), it should be self.superview instead of self.view.superview.

Realm - Get a list of all parents of all children from the child point of view

If I read that right, given a Player object, you want to retrieve a list of the Locomotive objects that are the parents of the Engine objects that are children of that Player object.

You should be able to do a lookup against the Locomotive object using the Player object's engines property. It would look something like this (Although admittedly, this is untested):

let player = // Get the player
let locomotives = realm.objects(Locomotive.self).filter("ANY self IN %@", player.engines)

Given a view, how do I get its viewController?

Yes, the superview is the view that contains your view. Your view shouldn't know which exactly is its view controller, because that would break MVC principles.

The controller, on the other hand, knows which view it's responsible for (self.view = myView), and usually, this view delegates methods/events for handling to the controller.

Typically, instead of a pointer to your view, you should have a pointer to your controller, which in turn can either execute some controlling logic, or pass something to its view.

Capturing touches on a subview outside the frame of its superview using hitTest:withEvent:

I have modified the accepted answer's code to be more generic - it handles the cases where the view does clip subviews to its bounds, may be hidden, and more importantly : if the subviews are complex view hierarchies, the correct subview will be returned.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

if (self.clipsToBounds) {
return nil;
}

if (self.hidden) {
return nil;
}

if (self.alpha == 0) {
return nil;
}

for (UIView *subview in self.subviews.reverseObjectEnumerator) {
CGPoint subPoint = [subview convertPoint:point fromView:self];
UIView *result = [subview hitTest:subPoint withEvent:event];

if (result) {
return result;
}
}

return nil;
}

SWIFT 3

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

if clipsToBounds || isHidden || alpha == 0 {
return nil
}

for subview in subviews.reversed() {
let subPoint = subview.convert(point, from: self)
if let result = subview.hitTest(subPoint, with: event) {
return result
}
}

return nil
}

I hope this helps anyone trying to use this solution for more complex use cases.

Swift - Get the parent of a Realm object; Always empty

Turns out there were a number of issues that I had to do to resolve this.

I was using XCTest and Realm was causing issues where there were multiple targets.

  1. Make all my model classes' public
  2. Remove the models from the test target, this included a file where the JSON data was being loaded into memory
  3. I had to write my data into Realm, which I had not done;

            let realm = try! Realm()

    try! realm.write {
    for parent:EYLocomotive in objects {
    for _ in stride(from:0, to: parent.qty, by: 1) {
    let engine : EYEngine = EYEngine.init()
    parent.engines.append(engine)
    }
    realm.add(parent)
    }
    }

jq - How do I print a parent value of an object when I am already deep into the object's children?

For a more generic approach, save the value of the "parent" element at the detail level you want, then pipe it at the end of your filter:

jq '. as $parent | .details.name | select(. == "James Brown") | $parent'

Of course, for the trivial case you expose, you could omit this entirely:

jq 'select(.details.name == "James Brown")'

Also, consider that if your selecting filters return many matches for a single parent object, you will receive a copy of the parent object for each match. You may wish to make sure your select filters only return one element at the parent level by wrapping all matches below parent level into an array, or to deduplicate the final result with unique.



Related Topics



Leave a reply



Submit