【问题标题】:want to call ViewController's method from appdelegate想从 appdelegate 调用 ViewController 的方法
【发布时间】:2014-11-18 14:31:45
【问题描述】:

我想在 AppDelegate.m 中调用ViewController 的方法。 我的Viewcontroller 中有方法method()。我希望在appdelegate.m 中调用didSelectMethod() 时调用它。 我已经调用了这样的方法。-

ViewController *vc=[[ViewController alloc]init];
[vc method];

方法被调用,但实例与实际方法不同。 它具有所有 nill 值。谁能给我我想要的正确代码。

谢谢 贾格维尔拉纳

【问题讨论】:

  • 你在使用故事板吗?
  • 是的,我正在使用故事板。
  • 谢谢大家。最后我解决了。 thnx GAD
  • @JagveerSingh 而不是将两个文件相互导入,您应该在一个文件中使用前向声明(通常是一种好习惯)。
  • 我可以用代码告诉我吗?我不明白

标签: ios objective-c methods uiviewcontroller appdelegate


【解决方案1】:

虽然对于视图控制器是 rootViewController 的情况已经正确回答了这个问题,但为了完整起见,以下是如何让它与任何视图控制器一起工作:

// ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (void)myMethod;

@end

// ViewController.m

#import "ViewController.h"
#import "AppDelegate.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.myViewController = self;

}

- (void)myMethod
{
    NSLog(@"Doing something interesting");
}

// AppDelegate.h

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (weak, nonatomic) ViewController *myViewController;

@end

// AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidEnterBackground:(UIApplication *)application {

    [self.myViewController myMethod];

}

【讨论】:

    【解决方案2】:

    在您的应用程序中获取相同的 anyViewController 实例的最简单方法是逐个跟踪它... 就像您应用中的任何 viewController 一样

    [[[UIApplication sharedApplication] keyWindow] rootViewController];
    

    或来自 appDelagate

    self.window.rootViewController;
    

    它会给你 rootViewController 然后从这个 rootViewController 跟踪你想要的 viewController。

    【讨论】:

      【解决方案3】:

      在您的问题中,您创建了一个不属于视图层次结构的新视图控制器实例,因此您不会通过调用该方法(UI 方面)看到任何效果。 (而且它不是通过 xib/storyboard 初始化的,所以你的 UI 元素可能是 nil)

      只要它是根视图控制器,您就可以通过窗口访问它(否则您必须在创建时找到它或保留对它的引用):

      ViewController *vc = (ViewController *)self.window.rootViewController;
      [vc method]; 
      

      【讨论】:

        【解决方案4】:

        首先你在 Appdelegate.h 中声明你的 ViewController 类,并在 AppDelegate.h 中创建你的 UIViewController 类的对象,像这样

        @class yourViewControllerClass;
        @property (nonatomic,strong) yourViewControllerClass *obj1;
        

        现在在 AppDelegate.m 中导入你的 ViewController 类,像这样

        #import yourViewControllerClass.h;
        

        现在在 AppDeledate.m 的方法 applicationDidFinishLaunchingOption 中:创建 viewController 的新对象并将其分配给 obj1 ,就像这样

        yourViewControllerClass *obj2 = [[yourViewControllerClass alloc]init];
        obj2 = self.obj1 
        

        借助此代码,您可以解析视图控制器或对象之间的数据。现在,您必须在 ViewController 类的 .m 文件中导入 Appdelegate.h 并将 viewController 对象中的所有数据解析为 obj1,然后 obj1 将解析该数据到obj2(使用上面代码的helo)。[假设您的viewController类的对象为OBJ]。

        #import AppDelegate.h
        AppDelegate *ad = [[AppDelegate alloc]init];
        ad.obj1 = OBJ
        

        注意-我尚未测试此代码..请先将您的项目保存在其他地方..根据我的知识回答..希望这会对您有所帮助..谢谢

        【讨论】:

        • [[AppDelegate alloc]init] !!??
        • 我之前使用此代码在我的 ViewController 之间传递一些数据...我之前没有尝试过使用 AppDelegate..但我认为这会起作用..至少你可以知道如何解析数据
        【解决方案5】:

        在.pch文件中写下这段代码

        #import "AppDelegate.h"
        #define DELEGATE ((AppDelegate*)[[UIApplication sharedApplication]delegate])
        

        在任何 ViewController 中你想调用 Delegate 的方法然后简单地编写这段代码。

        [DELEGATE methodname];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-15
          相关资源
          最近更新 更多