iOS - 从 ViewController 调用 App Delegate 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5082738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:58:53  来源:igfitidea点击:

iOS - Calling App Delegate method from ViewController

iosobjective-cuiviewcontrollerappdelegate

提问by Mytheral

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.

我想要做的是单击一个按钮(在代码中创建)并让它调用一个不同的视图控制器,然后让它在新的视图控制器中运行一个函数。

I know it could be done relatively easily in IB but that isn't an option.

我知道它可以在 IB 中相对容易地完成,但这不是一个选择。

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.

我想要做的一个例子是,如果你有两个视图控制器,一个带有房子的闪屏。另一个视图控制器可以在上面穿过房子,您可以按照设定的顺序穿过所有房间。启动画面将为每个房间提供按钮,让您可以在步行过程中跳转到任何点。

回答by Cristian Radu

You can access the delegate like this:

您可以像这样访问委托:

MainClass *appDelegate = (MainClass *)[[UIApplication sharedApplication] delegate];

Replace MainClasswith the name of your application class.

MainClass替换为您的应用程序类的名称。

Then, provided you have a property for the other view controller, you can call something like:

然后,如果你有另一个视图控制器的属性,你可以调用类似的东西:

[appDelegate.viewController someMethod];

回答by canhazbits

And if anyone is wondering how to do this in swift:

如果有人想知道如何做到这一点swift

if let myDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
   myDelegate.someMethod()
}

回答by sradforth

Sounds like you just need a UINavigationControllersetup?

听起来你只需要一个UINavigationController设置?

You can get the AppDelegateanywhere in the program via

您可以通过以下方式获取AppDelegate程序中的任何位置

YourAppDelegateName* blah = (YourAppDelegateName*)[[UIApplication sharedApplication]delegate];

In your app delegate you should have your navigation controller setup, either via IB or in code.

在您的应用委托中,您应该通过 IB 或代码设置导航控制器。

In code, assuming you've created your 'House overview' viewcontroller already it would be something like this in your AppDelegatedidFinishLaunchingWithOptions...

在代码中,假设您已经创建了“房屋概览”视图控制器,那么在您的AppDelegatedidFinishLaunchingWithOptions...

self.m_window = [[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds] autorelease];
self.m_navigationController = [[[UINavigationController alloc]initWithRootViewController:homeViewController]autorelease];
[m_window addSubview:self.m_navigationController.view];

After this you just need a viewcontroller per 'room' and invoke the following when a button click event is picked up...

在此之后,您只需要每个“房间”一个视图控制器,并在选择按钮单击事件时调用以下内容...

YourAppDelegateName* blah = (YourAppDelegateName*)[[UIApplication sharedApplication]delegate];
[blah.m_navigationController pushViewController:newRoomViewController animated:YES];

I've not tested the above code so forgive any syntax errors but hope the pseudo code is of help...

我还没有测试过上面的代码,所以请原谅任何语法错误,但希望伪代码有帮助......

回答by mikemike396

This is how I do it.

我就是这样做的。

[[[UIApplication sharedApplication] delegate] performSelector:@selector(nameofMethod)];

Dont forget to import.

不要忘记导入。

#import "AppDelegate.h"

回答by Tunvir Rahman Tusher

Just Follow these steps

只需按照以下步骤

1.import your app delegate in your class where you want app delegate object.

1.在您想要应用程序委托对象的类中导入您的应用程序委托。

#import "YourAppDelegate.h"

2.inside your class create an instance of your app delegate object(Its basically a singleton).

2.在你的类中创建一个你的应用程序委托对象的实例(它基本上是一个单例)。

YourAppDelegate *appDelegate=( YourAppDelegate* )[UIApplication sharedApplication].delegate;

3.Now invoke method using selector

3.现在使用选择器调用方法

if([appDelegate respondsToSelector:@selector(yourMethod)]){

        [appDelegate yourMethod];
    }

or directly by

或直接通过

[appDelegate yourMethod];

for swift

为了迅速

let appdel : AppDelegate = UIApplication.shared.delegate as! AppDelegate

i will recommend the first one. Run and Go.

我会推荐第一个。跑去。

回答by bruno.yvan.morel

NSObject <UIApplicationDelegate> * universalAppDelegate = 
    ( NSObject <UIApplicationDelegate> * ) [ [ UIApplication sharedApplication ] delegate ];

It avoid having to include your AppDelegate.h everywhere. It's a simple cast that goes a long way, allowing to develop independent Controller and reuse them elsewhere without to worry about class name and so on...

它避免了在任何地方都包含你的 AppDelegate.h。这是一个简单的转换,有很长的路要走,允许开发独立的 Controller 并在其他地方重用它们,而不必担心类名等等......

Enjoy

享受

回答by ZaEeM ZaFaR

A lot of good answers are already added. Though I want to add something which suits me most of the time.

已经添加了很多好的答案。虽然我想添加一些最适合我的东西。

#define kAppDelegate ((YourAppDelegate *)[[UIApplication sharedApplication] delegate]);

and thats it. Use it throughout the application just like a constant.

就是这样。像常量一样在整个应用程序中使用它。

e.g.

例如

[kAppDelegate methodName];

Don't forget to import yourAppDelegate.h in corresponding .pch or macros file.

不要忘记在相应的 .pch 或宏文件中导入 yourAppDelegate.h。

回答by Daniele D.

If someone need the same in Xamarin (Xamarin.ios / Monotouch), this worked for me:

如果有人在 Xamarin (Xamarin.ios / Monotouch) 中需要同样的东西,这对我有用:

var myDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;

(Require using UIKit;)

(需要使用 UIKit;)

回答by Marco Miltenburg

And in case you need access to your WatchKit extension delegate from a view controller on watchOS:

如果您需要从 watchOS 上的视图控制器访问您的 WatchKit 扩展委托:

extDelegate = WKExtension.sharedExtension().delegate as WKExtensionDelegate?

回答by iAj

Update for Swift 3.0 and higher

Swift 3.0 及更高版本的更新

//
// Step 1:- Create a method in AppDelegate.swift
//
func someMethodInAppDelegate() {

    print("someMethodInAppDelegate called")
}

calling above method from your controller by followings

通过以下方式从您的控制器调用上述方法

//
// Step 2:- Getting a reference to the AppDelegate & calling the require method...
//
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {

    appDelegate.someMethodInAppDelegate()
}

Output:

输出:

enter image description here

在此处输入图片说明