I try to run function on another class, But have a suspicious nil in code.

Hello! I'm a newbie swift dev! I try to create a project recently, but I encountered strange problems often for no reason.

I have a button to change pages, but my page uses UIViewContainer for page classification, If user will change page. They will touch button in UIViewContainer class "Home" for tell to viewController(the mastermind class of all page) for use change page function, after that page will change. I have some tools for try to use function in ViewController for test, and It worked!

But the problem is this. I don't know how I use function over page. If now user is stay a HomeController. will have to command skip class for use function on viewController. I find this code for that, But have a error.

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

I don't know this is a program restrictions or code have a bug. I think when I use this code, I can call the function on viewController for change page. but have a error in function.

yeah... in function. If base on that image. I have this bug in UIViewContoller class "MyClass"(It's can call the function in UIViewContoller when user touch button in ViewContainer). and have a bug in addChild(controllerTwo!).

Have a way for fix? or have a way for use function on another class? I don't see how to do that on website and books. I don't know what this name of code.

and thank you for answer! and so very sorry for my strange English. 😅

Answered by SSRtnz in 752534022

I think it's like this code. But when I doing, It's have nil.

stack overflow : How to call a function from a different swift file

Have a way for fix the nil? or this code is old?

Accepted Answer

I think it's like this code. But when I doing, It's have nil.

stack overflow : How to call a function from a different swift file

Have a way for fix the nil? or this code is old?

I try to run function on another class, But have a suspicious nil in code.
 
 
Q