In my app I have a “main” ViewController
. On app launch in the body of its viewDidAppear
, I retrieve the value of a UserDefaults boolean
: if the user is not logged in, I present modally another viewcontroller
this way:
self.performSegue(withIdentifier:"loginView", sender:self)
This launches modally AuthViewController
, a viewcontroller that contains a “Container View”, a region of AuthViewController that includes a UIPageViewController
(AuthPageViewController
).
This last one has two “pages”: LoginViewController
and RegisterViewController
.
LoginViewController
is used to login users: once the user is logged in I want to call a function (no matter what it does) on the main ViewController
.
So, to summarize, I’m in LoginViewController
and I want to call a method of ViewController
.
While trying a solution I discovered the following:
- I can’t get a reference to
presentingViewController
becauseLoginViewController
has not been opened directly from ViewController. Moreover,AuthViewController
was not launched withpresent(_:animated:completion:)
but withself.performSegue
as I said before; - instantiating a
ViewController
fromLoginViewController
and calling a method works but it’s not a good idea; NSNotification
works like a charm;- apparently delegates aren’t working or I’m missing something about their implementation in the contest of this app;
Can you help me understand how to call a ViewController method from my LoginViewController? Code examples are very well received, but I appreciate any advice.
Let me explain it with code of how to use delegate in this case,
First you need create a delegate in
LoginViewController
like thisthen create a variable for delegate like this
Now in you
ViewController
you have to assign thedelegate
like this in your prepareprepareforsegue
method, as you are using segue like thisand implement that method like this
in this way you are able to call
goToContent
fromLoginViewController