iOS – 从ViewController调用App Delegate方法
我想要做的是点击一个button(它是在代码中创build的),让它调用一个不同的视图控制器,然后让它在新的视图控制器中运行一个函数。
我知道在IB可以做得相对容易,但这不是一个select。
我想要做的一个例子是,如果你有两个视图控制器,一个房子的闪屏。 另一个视图控制器已经走过了房子,你可以按照设定的顺序通过所有的房间。 启animation面将有每个房间的button,让你跳到任何点的步行。
你可以像这样访问委托:
MainClass *appDelegate = (MainClass *)[[UIApplication sharedApplication] delegate]; 将MainClassreplace为您的应用程序类的名称。
然后,如果您有其他视图控制器的属性,则可以调用如下所示的内容:
 [appDelegate.viewController someMethod]; 
 听起来像你只需要一个UINavigationController设置? 
 你可以通过程序中的任何地方获取AppDelegate 
 YourAppDelegateName* blah = (YourAppDelegateName*)[[UIApplication sharedApplication]delegate]; 
在您的应用程序委托中,您应该通过IB或代码来设置您的导航控制器。
 在代码中,假设你已经创build了“House overview”viewcontroller,那么你的AppDelegate didFinishLaunchingWithOptions 。 
 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]; 
在此之后,您只需要每个“房间”的视图控制器,并在button单击事件被选中时调用以下内容…
 YourAppDelegateName* blah = (YourAppDelegateName*)[[UIApplication sharedApplication]delegate]; [blah.m_navigationController pushViewController:newRoomViewController animated:YES]; 
我没有testing上面的代码,所以原谅任何语法错误,但希望伪代码是帮助…
 如果有人想知道如何swift做到这一点: 
 if let myDelegate = UIApplication.sharedApplication().delegate as? AppDelegate { myDelegate.someMethod() } 
这是我如何做到的。
 [[[UIApplication sharedApplication] delegate] performSelector:@selector(nameofMethod)]; 
不要忘记导入。
 #import "AppDelegate.h" 
 NSObject <UIApplicationDelegate> * universalAppDelegate = ( NSObject <UIApplicationDelegate> * ) [ [ UIApplication sharedApplication ] delegate ]; 
它避免了无处不在你的AppDelegate.h。 这是一个很简单的演员,可以开发独立的控制器并在其他地方重复使用,而不必担心类名等等。
请享用
只要按照这些步骤
1.在您的应用程序委托对象的类中导入您的应用程序委托。
 #import "YourAppDelegate.h" 
2.在你的类中创build你的应用程序委托对象的实例(它基本上是一个单例)。
 YourAppDelegate *appDelegate=( YourAppDelegate* )[UIApplication sharedApplication].delegate; 
3.现在使用select器调用方法
 if([appDelegate respondsToSelector:@selector(yourMethod)]){ [appDelegate yourMethod]; } 
或直接通过
 [appDelegate yourMethod]; 
为迅速
 let appdel : AppDelegate = UIApplication.shared.delegate as! AppDelegate 
我会推荐第一个。 跑,走。
很多好的答案已经被添加了。 虽然我想补充一些很适合我的东西。
 #define kAppDelegate ((YourAppDelegate *)[[UIApplication sharedApplication] delegate]); 
这就是它。 就像常量一样,在整个应用程序中使用它。
例如
 [kAppDelegate methodName]; 
不要忘记在相应的.pch或macros文件中导入yourAppDelegate.h。
如果有人需要在Xamarin(Xamarin.ios / Monotouch)相同,这对我工作:
 var myDelegate = UIApplication.SharedApplication.Delegate as AppDelegate; 
(需要使用UIKit;)
如果您需要从watchOS上的视图控制器访问您的WatchKit扩展委托,请执行以下操作:
 extDelegate = WKExtension.sharedExtension().delegate as WKExtensionDelegate? 
 您可以在项目的Prefix.pch文件中添加#define uAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate] ,然后用下面的代码调用任何UIViewController AppDelegate任何方法。 
 [uAppDelegate showLoginView]; 
更新Swift 3.0和更高版本
 // // Step 1:- Create a method in AppDelegate.swift // func someMethodInAppDelegate() { print("someMethodInAppDelegate called") } 
通过以下方式从您的控制器调用以上方法
 // // Step 2:- Getting a reference to the AppDelegate & calling the require method... // if let appDelegate = UIApplication.shared.delegate as? AppDelegate { appDelegate.someMethodInAppDelegate() } 
输出:
 