2014-09-24 115 views
1

我想在AppDelegate.m中调用ViewController的方法。 我在我的Viewcontroller。中有方法method(),我希望在appdelegate.m中调用didSelectMethod()时调用它。 我呼吁方法类似this.-想要从appdelegate调用ViewController的方法

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

方法被调用,但没有相同的实例作为实际的方法有。 它具有所有nill值。任何人都可以为我提供正确的代码。

谢谢 Jagveer林蛙

+0

你在使用故事板吗? – 2014-09-24 07:38:20

+0

是我使用故事板。 – 2014-09-24 09:23:05

+0

谢谢大家。最后我解决它。 thnx GAD – 2014-09-24 09:23:40

回答

3

虽然这已经正确地回答了在视图控制器是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]; 

} 
1

在你的问题,你创建一个新的视图控制器实例,它是不是视图层次的一部分,所以你不会看到通过调用方法(UI明智)任何影响。 (也没有通过xib/storyboard初始化,所以你的UI元素可能为零)

只要它是根视图控制器就可以通过窗口访问它(否则你必须找到它或保留一个当创建参考吧):

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

得到anyViewController的同一实例在您的应用程序最简单的方法是跟踪一个一个...从任何的viewController 在自己应用

[[[UIApplication sharedApplication] keyWindow] rootViewController]; 

或来自appDelagate

self.window.rootViewController; 

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

1

首先,你必须声明你的ViewController在Appdelegate.h类,并在AppDelegate.h你的UIViewController类的一个对象,这样

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

现在导入您的ViewController类AppDelegate.m,这样

#import yourViewControllerClass.h; 

现在AppDeledate.m的方法applicationDidFinishLaunchingOption:创建您的viewController的一个新对象,并将其分配给OBJ1,这样

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

有了这个代码的帮助,您可以解析viewcontrollers或之间的数据objects.Now在您的ViewController类的.m文件中,您必须导入Appdelegate.h并将您的viewController对象中的所有数据解析为obj1,并且obj1会将该数据解析为obj2(使用上述代码的helo)。你的viewController类为OBJ]。

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

注意 - 我没有测试过这code..please保存项目根据我knowledge..hope这将有助于第一某处else..answered you..Thank你

+0

[[AppDelegate alloc] init] !! ?? – 2014-09-24 08:28:46

+0

我以前使用此代码之间传递我的ViewControllers之间的一些数据...我还没有尝试与AppDelegate之前......但我认为这将工作..至少你可以得到一个想法如何解析数据 – 2014-09-24 08:33:53

0

在。pch文件中写入代码

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

在任何您想调用Delegate方法的ViewController中,只需编写此代码即可。

[DELEGATE methodname]; 
相关问题