【发布时间】:2015-06-22 02:43:05
【问题描述】:
当在 FirstViewController 中按下按钮时,我希望它在另一个类 (SecondViewController) 中调用 funcpressedButton()。我该怎么做?
我尝试了以下方法,但它不能正常工作:
SecondViewController().pressedButton()
这里是pressedButton的代码:
func pressLearnButton() {
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
self.scrollView.setContentOffset(CGPoint(x: screenWidth, y: 0), animated: true)
}
【问题讨论】:
-
您能否更具体地说明您要实施的内容?它可能有助于回答您的问题。例如,您是否使用 segue 在 FirstViewController (FVC) 和 SVC 之间进行切换?还是 SVC 存在于 FVC 中?
-
这是可能的(下面给出的答案是变通方法,可能有一些优点,但我认为这不是您想要的),但我们必须知道您的视图层次结构才能帮助您.
SecondViewController和FirstViewController是什么关系? -
在我看来,您似乎还不了解面向对象编程的一些概念。这还不错,每个人都从某个地方开始。我建议您阅读 OOP 的基础知识,以了解类和对象之间的区别,如何引用其他对象并向它们发送消息。
-
您没有提供足够的信息。 FirstViewController 的实例是如何出现在屏幕上的?屏幕上是否已经存在 SecondViewController 的实例(可能在导航控制器堆栈的更下方,或者从 SecondViewController 的实例到 FirstViewController 的实例的模态
presentViewController:animated:completion:?)或者您是否需要创建 SecondViewController 的新实例? -
在故事板上,所有的视图都没有连接。 SecondViewController 有一个 UIScrollView,我在其中将 FirstViewController 和其他人添加到它的子视图中。我正在尝试在 FirstViewController (滚动视图的第一个控制器)上按下一个按钮,使滚动视图移动到某个偏移量。滚动视图在 SecondViewController 中。