Can't Access View Controller

Hi Apple Dev Community,
I'm moving along with my project, whose hierarchy is getting more comlicated by day.
At this point, it has become necessary to programmatically access the top level view controller from within a child view.
My first approach was to create a custum init( ) method for the child view, and then pass the view controller as a parameter in the said init( ) method.
However, my research suggests that that is a bad programming practice.
I have found many articles that show how to programmatically add a child view controller to a parent view controller, but could not find how to add a child view to a parent view controller.


Again, I need a valid reference to the parent view controller from within the child view that doesn't cause the program to explode.

Does anyone know how to achieve this?
Many thanks in advance! 🙂
KC

Replies

Please show your code.

You have the parent property of a view that gives you this parent controller.


weak var parent: UIViewController? { get }

Discussion

If the recipient is a child of a container view controller, this property holds the view controller it is contained in. If the recipient has no parent, the value in this property is

nil
.

Prior to iOS 5.0, if a view did not have a parent view controller and was being presented, the presenting view controller would be returned. On iOS 5, this behavior no longer occurs. Instead, use the

presentingViewController
property to access the presenting view controller.

Call me a noob if you have to (which am), but if I add

weak var parent: UIViewController? { get }

to my code, I get a build-time error "Expected '{' to start getter definition"
But if I delete the above line, I then get the error 'Use of unresolved identifier 'parent'

Clearly, I must be doing something wrong?
I did explicitly added the child view in the parent view controller by calling

self.view.addSubview(mySubView)

It is a notation to be used in a documentation, `weak var parent: UIViewController? { get }`.

Which is not intended to be an actual code added.


And that resides in the documention of UIViewController, not in UIView.

(I assume you distinguish view controllers and views properly.)


If you have shown enough code, you would not see such confusing replies.

Can have a look here, if you need the parent of a view (not viewControiller)


h ttps://www.hackingwithswift.com/example-code/uikit/how-to-find-the-view-controller-responsible-for-a-view


For a viewController, call the parent propertry on the child view controller.

Hi Ooper.

I've boiled my project down to only its most salient components.
Below is a link to my project. This way, you can see you see my .xib in action in addtion to my source code.

workupload [DoT} c0m SlAsH file sLaSh jKSaNzJJ
As you can see, when you try to run my code, you get an
[ViewController collectionView:numberOfItemsInSection]: unrecognized selector sent to instance 0x7fcadfc0d690


error.
I am not sure what this means or what is causing this.
Any help is much appreciated!

Could you post the code anyway. That avoids having to go and download.


Are the other collectionView selectors working correctly ?

Have you checked that delegate and dataSource are correctly set ?

Sorry, but my company does not permit us to download codes from arbitrary sites. If you cannot show us your code as text, I cannot be much help of you. Hoping your issue would be solved soon.