AppDelegate调用 ViewController的方法,

2020-07-22  本文已影响0人  GatesLand

一. 在 ViewController里调用AppDelegate的属性和方法

  1. 首先需要在APPdelegate.h里声明属性和方法,如下:在APPdelegate里原有的属性window,和新加的一个方法 -(void)loadOtherLoginView;


    image
  2. 在viewcontrol里使用,先导入头文件#import “AppDelegate.h”,然后,调用方法

AppDelegate* ad = (AppDelegate*)[UIApplication sharedApplication].delegate;      
 //调用loadOtherLoginView方法
 [ad loadOtherLoginView];
 //使用window的属性
 ad.window.rootViewController = [[LoginViewController alloc]init];

二. 在 AppDelegate里调用ViewController的属性和方法

  1. 首先在ViewController.h中(我这里以viewcontrol为例,你们可以在其他control里s使用)添加属性和方法:一个属性myStr;一个方法- (NSString *)getIphoneLaunchImageName。


    image
  2. 热后在ViewController.m里实现方法内容,


    image
  3. 在ViewController.m中导入APPdelegate的头文件 #import “AppDelegate.h"
  4. 在viewDidLoad方法里或者其他方法里实现APPdelegate的属性赋值,如下:


    image
  5. 在进行上面的第四步APPdelegate的属性赋值时,需要在APPdelegate.h 声明属性如下:


    image
  6. 最后就可以在APPdelegate.m的方法里调用viewcontrol的方法了。我这里是在APP进去前台的方法里调用,你也可以自己定义调用的方法的时间。(但需要注意的是:需要ViewController加载过后,在调用其里面的方法)。
    image
    如果有什么不懂的和问题,可以参考下我写的demo,欢迎下方评论和指正
    参考链接:从appdelegate调用ViewController的方法.
上一篇下一篇

猜你喜欢

热点阅读