'NSInvalidArgumentException' / '-[AppDelegate fieldChanged:]: unrecognized selector sent to instance - iphone

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];

Related

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.
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

Conform id to protocol, is it needed?

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

How to access delegate method from another class?

I have a method in my delegate that does this:
-(void)showAddingPersonalDetails; {
personal = [[AddingPersonalDetails alloc] initWithNibName:#"AddingWithPersonalDetails" bundle:nil];
[window addSubview:personal.view];
[window makeKeyAndVisible];
mainscreen.view.hidden = YES;
NSLog(#"Called");
}
I don't want this view initialized until I need it. That's why put in in a method.
The problem is, I can't seem to access this code from another class.
I even tried this:
BitWiseAppDelegate *appDelegate = (BitWiseAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.showAddingPersonalDetails;
But it doesn't work. Any ideas?
try with following code;
BitWiseAppDelegate *appDelegate = (BitWiseAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate showAddingPersonalDetails];
or
[(BitWiseAppDelegate *)[[UIApplication sharedApplication] delegate] showAddingPersonalDetails];

UIApplication sharedApplication - keyWindow is nil?

I want to convert a CGPoint from my UIView to UIWindow coordinates and have realized that UIApplication keyWindow is always nil; why is this?
I have tried the convertPoint:toView: method from UIView.
Please see this sample code I tried in the view controller in a template of Xcode (View application):
- (void)viewDidLoad {
[super viewDidLoad];
UIView *test = [[UIView alloc] initWithFrame:CGRectMake(40,40,250,250)];
[test setBackgroundColor:[UIColor redColor]];
[self.view addSubview:test];
CGPoint p = CGPointMake(100, 100);
CGPoint np;
np = [test convertPoint:p toView:[[UIApplication sharedApplication] keyWindow]];
NSLog(#"p:%# np:%#", NSStringFromCGPoint(p), NSStringFromCGPoint(np));
AppDelegate *appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;
np = [test convertPoint:p toView:[appDel window]];
NSLog(#"p:%# np:%#", NSStringFromCGPoint(p), NSStringFromCGPoint(np));
np = [test convertPoint:p toView:nil];
NSLog(#"p:%# np:%#", NSStringFromCGPoint(p), NSStringFromCGPoint(np));
[test release];
if(![[UIApplication sharedApplication] keyWindow])
NSLog(#"window was nil");
}
and I get:
p:{100, 100} np:{100, 100}
p:{100, 100} np:{140, 160}
p:{100, 100} np:{100, 100}
window was nil
I can convert it but only when I access the window through the app delegate. And not UIApplication. According to the documentation, keyWindow should work here, but is nil.
Why is this?
This code was executed before [window makeKeyAndVisible]; which is inside the app delegate.
So, no wonder why keyWindow was nil yet.
Easiest way is to get the window from the app delegate instead:
UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
// Do something with the window now
I noticed that after having started the Guided Access, the keyWindow property on [UIApplication sharedApplication] appears to be nil.
It happened to me only on iOS7 when starting the Guided Access Mode for the first time after having enabled it in Settings > General > Guided Access, so the starting GAM view is actually displayed and not by-passed.
Since this Apple API seems buggy, I solved using the following code to retrieve the window I'm looking for.
NSArray *windows = [[UIApplication sharedApplication] windows];
if ([windows count]) {
return windows[0];
}
return nil;
Instead of
[[UIApplication sharedApplication] keyWindow];
maybe you could also try using
[[[UIApplication sharedApplication] delegate] window];
as iWasRobbed pointed out but it wasn't working for me as the rootViewController property isn't reachable this way.
Try this, first get the UINavigationController handle, and then the topViewController
let navController = window?.rootViewController as! UINavigationController
let yourMainViewController = navController.topViewController as! ItemsViewController
or
let yourMainViewController = navController.viewControllers.first as! ItemsViewController

UIApplication sharedAppication error: program seems to be accessing wrong file

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;
}