I have just been looking at some old code and it got me thinking about which of these to use, both seem to work without complaint. I was just curious when I spotted the difference.
THIS:
id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
OR
id appDelegate = [[UIApplication sharedApplication] delegate];
i guess it depends on what you are asking to appDelegate
i mean, if you are going use the property "window" which is defined in UIApplicationDelegate protocol:
NSLog(#"%#", appDelegate.window);
then you should use :
id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
but if you try:
id appDelegate = [[UIApplication sharedApplication] delegate];
NSLog(#"%#", appDelegate.window);
you'll get an error...
While meronix's answer is correct, it misses an important point.
You should always declare variables to have the most specific type possible.
By doing so, you give the compiler the maximum amount of information with which to validate your code. Thus, this is preferable because it tells the compiler to limit the search for selectors to a minimal number:
id <UIApplicationDelegate> appDelegate = ...;
Note that id<SomeProtocol> limits the set of valid selectors to only those that exist in SomeProtocol. This why you'll sometimes see the protocol declared as also implementing <NSObject> or you'll see id<SomeProtocol, NSObject> (or NSObject<SomeProtocol>*) as the type declaration.
Try this..
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication]delegate];
to avoid any kind of warning
Related
I'm trying to call a method that's inside someClass from the AppDelegate class.
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.
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".
Any light shed on the matter would be greatly appreciated! :)
[[UIApplication sharedApplication] delegate]; return your AppDelegate class , not SomeClass
you can use like this :
FFAppDelegate *delegate = (FFAppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate someMethodForSomeClass];
And then in your AppDelegate code someMethodForSomeClass like this :
- (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). 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
-(void)someMethod;
So anyone could access it.
For Example if you want to Create AlertView only Once and Use it in any UiViewController
So, you can make Method For UIAlertView and Call the Method When you Want
1)In your appDelegate.h file
#property (nonatomic, strong) UIAlertView *activeAlertView;
2) In your AppDelegate.m File
-(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
[((AppDelegate *)[[UIApplication sharedApplication] delegate])openAlert];
Note: Define -(void)openAlert Method in Appdelegate.h file
In my project it says Assigning to 'AppDelegate *' from incompatible type 'id'.
What exactly is this? Why did this warning occur?
I have declared in .m
AppDelegate *appdev;
and in viewDidLoad
{
appdev = [[UIApplication sharedApplication]delegate]; <= warning here
}
I want to hide this warning. What should I do? Thanks in advance.
since you know they equal, add a cast to let the compiler know
AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication]delegate];
since this might come up in Swift too
let app = UIApplication.shared.delegate as! AppDelegate
If you want you have this for import AppDelegate anywhere. Is simple.
IN APPDELEGATE
/**
* Get AppDelegate
* Call [AppDelegate getAppDelegate]
*
* #return AppDelegate
*/
+ (AppDelegate *) app {
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
ANYWHERE IN YOUR CLASS
#import "AppDelegate.h" // TOP OF YOUR CLASS
AppDelegate *app = [AppDelegate app];
You can type-cast it to prevent the warning message.
try:
appdev = (AppDelegate *)[[UIApplication sharedApplication] delegate];
You need to type cast because it returns Protocol.
appdev = (AppDelegate *)[[UIApplication sharedApplication] delegate];
The app terminates with a NSInvalidArgumentException when I use this code and I do not know why:
In my FieldViewController.m I have:
-(IBAction)fieldEntered:(NSString*)sender {
[self.temp resignFirstResponder];
NSString *setFieldEntered;
setFieldEntered = temp.text;
fieldTemp = setFieldEntered;
[(EditViewController *)[[UIApplication sharedApplication] delegate] fieldChanged:(id)sender];
[self dismissModalViewControllerAnimated:YES];
}
It terminates on the [(EditViewController *)[[UIApplication sharedApplication] delegate] fieldChanged:(id)sender]; line.
In my EditViewController.h I have -(void)fieldChanged:(id)sender; and in my EditViewController.m file I have:
-(void)fieldChanged:(id)sender {
[fieldArray insertObject:[FieldViewController fieldEntered] atIndex:[fieldArray count]+1];
}
Please help me figure this one out. Thank you.
From the error, it would appear that the UIApplication's delegate is set to an instance of a class called AppDelegate, whereas you're treating it as a different class called EditViewController.
That doesn't make sense. May be you forgot to refer to the controller. Should be something like this:
[[[UIApplication sharedApplication] delegate].editViewController fieldChanged:sender];
I have started with iPhone development sometime back and I am trying to implement core data in my application.
In the process of executing FetchRequest I am stuck at following code...
MYAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
While debugging the following error is displayed...
Program received signal: "EXC_BAD_ACCESS"
When I run the app, it just crashes.
This error appears again and again when I press 'continue' button in debug mode.
I tried changing my code, to this.....
NSManagedObjectContext *context = [(MyAppDelegate *)[[UIApplication sharedApplication\ delegate] managedObjectContext];
This lets the app run but when I press the Simulator home button, the same error is displayed in console.
What could be going wrong over here?
Make sure you have a public accessor method for your application delegate. I would implement it like so, at the top of your AppDelegate.m
+ (MYAppDelegate *)sharedAppDelegate
{
return (MYAppDelegate *) [UIApplication sharedApplication].delegate;
}
You may then access it using:
[[MYAppDelegate sharedAppDelegate] managedObjectContext]
in my MainViewController implementation, I need to access variables from two different classes. one of the classes is the AppDelegate and the other is the FlipsideViewController.
the way I accessed these was through this code:
-(void)someMethod
{
MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
FlipsideViewController *viewController = (FlipsideViewController *)[[UIApplication sharedApplication] delegate];
then I have an array I access from my application delegate, and some instance variables that return values from an instance of UISwitch from the flipsideViewController:
NSMutableArray* array = [[NSMutableArray alloc] initWithArray:(NSMutableArray *)appdelegate.originalArray];
for (id element in array)
{
if ([[element attribute] isEqualToString:#"someAttribute"] && [viewController.switch1 isOn] == YES)
{
//preform function
}
}
I keep getting the error message "-[MyApplicationAppDelegate switch1]: unrecognized selector sent to instance. terminating app due to uncaught exception"
[[UIApplication sharedApplication] delegate]; will always return the (singleton) instance of MyApplicationAppDelegate class and you cannot simply cast it to FlipsideViewController*. to access flipsidecontroller value (assuming it is stored in your appdelegate) you can define a property and call it:
-(void)somemethod{
MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
FlipsideViewController *viewController = appDelegate.flipsideController;
}