iOS - 从 ViewController 调用 App Delegate 方法 [英] iOS - Calling App Delegate method from ViewController
问题描述
我想要做的是单击一个按钮(在代码中创建)并让它调用一个不同的视图控制器,然后让它在新的视图控制器中运行一个函数.
What I am trying to do is click a button (that was created in code) and have it call up a different view controller then have it run a function in the new view controller.
我知道它可以在 IB 中相对容易地完成,但这不是一个选项.
I know it could be done relatively easily in IB but that isn't an option.
我想要做的一个例子是,如果你有两个视图控制器,一个带有房子的闪屏.另一个视图控制器可以在上面穿过房子,您可以按照设定的顺序穿过所有房间.启动画面将为每个房间提供按钮,让您可以在步行过程中跳转到任何点.
An example of what I want to do would be if you had two view controllers one with a splash screen of house. The other view controller had a walk through of the house on it that you could go through all the rooms in a set order. The splash screen would have buttons for each room that would allow you to jump to any point on the walk through.
推荐答案
您可以像这样访问委托:
You can access the delegate like this:
MainClass *appDelegate = (MainClass *)[[UIApplication sharedApplication] delegate];
将 MainClass 替换为您的应用程序类的名称.
Replace MainClass with the name of your application class.
然后,如果你有另一个视图控制器的属性,你可以调用类似的东西:
Then, provided you have a property for the other view controller, you can call something like:
[appDelegate.viewController someMethod];
这篇关于iOS - 从 ViewController 调用 App Delegate 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!