简体   繁体   English

从AppDelegate Objective-C IOS调用someClass内部的方法

[英]calling a method inside someClass from AppDelegate Objective-C IOS

I'm trying to call a method that's inside someClass from the AppDelegate class. 我正在尝试从AppDelegate类调用someClass内部的方法。 I usually create an instance and then using that instance, call the method. 我通常创建一个实例,然后使用该实例调用该方法。 Like so: 像这样:

FFAppDelegate *delegate = (FFAppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate someMethod];

I use that ^^ quite a bit in my code and it works perfectly. 我在代码中使用了很多^^,并且效果很好。 What I want to do, is switch it around. 我要做的是将其切换。 Instead of calling a method INSIDE the AppDelegate, I want to call a method inside another class FROM the AppDelegate. 我不想在AppDelegate内调用方法,而是要在AppDelegate的另一个类中调用方法。

SomeClass *test = (SomeClass *) [[UIApplication sharedApplication] delegate];
[test someMethod];

In this case, I keep getting "Terminating app due to uncaught exception 'NSInvalidArgumentException'" error due to "unrecognized selector sent to instance". 在这种情况下,由于“无法识别的选择器发送到实例”,我不断收到“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序”错误。

Any light shed on the matter would be greatly appreciated! 任何对此事的了解将不胜感激! :) :)

[[UIApplication sharedApplication] delegate]; return your AppDelegate class , not SomeClass 返回您的AppDelegate类,而不是SomeClass

you can use like this : 您可以这样使用:

FFAppDelegate *delegate = (FFAppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate someMethodForSomeClass];

And then in your AppDelegate code someMethodForSomeClass like this : 然后在您的AppDelegate代码中,如下所示的someMethodForSomeClass

- (void)someMethodForSomeClass
{
    SomeClass *someClass = _yourSomeClass;
    [someClass someMethod];
}

Instantiate an instance of the class that you want to send the request from and make the method public (in the .h file). 实例化要从中发送请求的类的实例,并使该方法公开(在.h文件中)。 Then import that class into the app delegate and call it. 然后将该类导入应用程序委托并调用它。

Like so... 像这样

  YourClass * yourClassInstance = [[YourClass alloc] init];
  [yourClassInstance someMethod];

in YourClass.h below the @interface you would declare the method like so 在@interface下的YourClass.h中,您将像这样声明方法

 -(void)someMethod;

So anyone could access it. 这样任何人都可以访问它。

For Example if you want to Create AlertView only Once and Use it in any UiViewController 例如,如果您只想创建一次AlertView并在任何UiViewController中使用它

So, you can make Method For UIAlertView and Call the Method When you Want 因此,您可以将方法设为UIAlertView并在需要时调用该方法

1)In your appDelegate.h file 1)在您的appDelegate.h文件中

@property (nonatomic, strong) UIAlertView *activeAlertView;

2) In your AppDelegate.m File 2)在您的AppDelegate.m文件中

-(void)openAlert
{
    NSString *strMsgPurchase = @"Write your message";
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Buy" message:strMsgPurchase delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy",@"Restore", nil];
    [alert setTag:100];
    [alert show];
    self.activeAlertView = alert;
}

3) To Call the Method in which uiview you want 3)调用所需的uiview方法

[((AppDelegate *)[[UIApplication sharedApplication] delegate])openAlert];

Note: Define -(void)openAlert Method in Appdelegate.h file 注意:在Appdelegate.h文件中定义-(void)openAlert方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM