一. 在 ViewController里調(diào)用AppDelegate的屬性和方法
-
首先需要在APPdelegate.h里聲明屬性和方法,如下:在APPdelegate里原有的屬性window,和新加的一個(gè)方法 -(void)loadOtherLoginView;
image 在viewcontrol里使用,先導(dǎo)入頭文件#import “AppDelegate.h”,然后,調(diào)用方法
AppDelegate* ad = (AppDelegate*)[UIApplication sharedApplication].delegate;
//調(diào)用loadOtherLoginView方法
[ad loadOtherLoginView];
//使用window的屬性
ad.window.rootViewController = [[LoginViewController alloc]init];
二. 在 AppDelegate里調(diào)用ViewController的屬性和方法
-
首先在ViewController.h中(我這里以viewcontrol為例,你們可以在其他control里s使用)添加屬性和方法:一個(gè)屬性myStr;一個(gè)方法- (NSString *)getIphoneLaunchImageName。
image -
熱后在ViewController.m里實(shí)現(xiàn)方法內(nèi)容,
image - 在ViewController.m中導(dǎo)入APPdelegate的頭文件 #import “AppDelegate.h"
-
在viewDidLoad方法里或者其他方法里實(shí)現(xiàn)APPdelegate的屬性賦值,如下:
image -
在進(jìn)行上面的第四步APPdelegate的屬性賦值時(shí),需要在APPdelegate.h 聲明屬性如下:
image - 最后就可以在APPdelegate.m的方法里調(diào)用viewcontrol的方法了。我這里是在APP進(jìn)去前臺(tái)的方法里調(diào)用,你也可以自己定義調(diào)用的方法的時(shí)間。(但需要注意的是:需要ViewController加載過(guò)后,在調(diào)用其里面的方法)。
image
如果有什么不懂的和問(wèn)題,可以參考下我寫(xiě)的demo,歡迎下方評(píng)論和指正
參考鏈接:從appdelegate調(diào)用ViewController的方法.