I have a UIViewController with an xib and using Interface Builder I’ve added a child UIView.
Within the child UIView, when I click on an object within that view, I want to be able to alter the title of the whole window.
Now I’d normally do that setting
self.title = @"hi";
on the parent UIViewController. But is there any way I can access the parent title from within the child?
I’ve tried
self.superview.title = @"i";
self.parentViewController.title = @"hi";
but neither work.
Any help much appreciated
thanks
self.superview.title = @"i";evaluates to an object of typeUIView, andUIViewhas notitleproperty.UIViewControllers have aparentViewControllerproperty butUIViews don’t.So the fundamental problem is that you’re not properly separating your controller and your view classes. What you’d normally do is make the view you want to catch taps on a subclass of
UIControl(which things likeUIButtonalready are, but if it’s a customUIViewsubclass then you can just change it into aUIControlsubclass sinceUIControlis itself a subclass ofUIView), then in your controller add something like:So you explicitly don’t invest the view with any knowledge about its position within the view hierarchy or how your view controllers intend to act. You just tell it to give you a shout out if it receives a user interaction.