问题描述
在我的应用程序中,我有一个主"ViewController
.在其 viewDidAppear
正文中启动应用程序时,我检索 UserDefaults 布尔值
的值:如果用户未登录,我会以模态呈现另一个 viewcontroller
这样:
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)
这以模态方式启动 AuthViewController
,一个包含Container View"的视图控制器,AuthViewController 的一个区域,包含一个 UIPageViewController
(AuthPageViewController
).
This launches modally AuthViewController
, a viewcontroller that contains a "Container View", a region of AuthViewController that includes a UIPageViewController
(AuthPageViewController
).
最后一个有两个页面":LoginViewController
和 RegisterViewController
.
This last one has two "pages": LoginViewController
and RegisterViewController
.
LoginViewController
用于登录用户:一旦用户登录,我想在主 ViewController
上调用一个函数(无论它做什么).
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
.
所以,总而言之,我在 LoginViewController
中,我想调用 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:
- 我无法获得对
presentingViewController
的引用,因为LoginViewController
尚未直接从 ViewController 中打开.此外,AuthViewController
不是用present(_:animated:completion:)
启动的,而是用self.performSegue
启动的,正如我之前所说; - 从
LoginViewController
实例化一个ViewController
并调用一个方法是可行的,但这不是一个好主意; NSNotification
就像一个魅力;- 显然代表没有工作,或者我在这个应用程序的比赛中遗漏了他们的实施;
- 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;
你能帮我理解如何从我的 LoginViewController 调用 ViewController 方法吗?代码示例很受欢迎,但我很感激任何建议.
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.
推荐答案
我用代码来解释一下这种情况下如何使用delegate,
Let me explain it with code of how to use delegate in this case,
首先你需要像这样在 LoginViewController
中创建一个委托
First you need create a delegate in LoginViewController
like this
protocol LoginViewControllerDelegate: class {
func goToContent(animated:Bool)
}
然后像这样为委托创建一个变量
then create a variable for delegate like this
weak var delegate: LoginViewControllerDelegate?
现在在您的 ViewController
中,您必须在准备 prepareforsegue
方法中像这样分配 delegate
,因为您正在使用这样的 segue
Now in you ViewController
you have to assign the delegate
like this in your prepare prepareforsegue
method, as you are using segue like this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourSegueIdentifier" {
let nextScene = segue.destination as! LoginViewController
nextScene.delegate = self
}
}
并像这样实现该方法
func goToContent(animated: Bool) {
print("test")
}
通过这种方式,您可以从 LoginViewController
in this way you are able to call goToContent
from LoginViewController
这篇关于从 UIViewController 调用 UIViewController 方法深深嵌套在模态中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!