How to access delegate method from another class? - iphone

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

Related

Assigning to 'AppDelegate *' from incompatible type 'id<UIApplicationDelegate>'

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

Why MBProgressHUD not show, when an alert show before HUD show?

I use this code to init MBProgressHUD
UIWindow *window = [[UIApplication sharedApplication] keyWindow]
_hud = [[MBProgressHUD alloc]initWithWindow:window];
_hud.dimBackground = bDim;
_hud.labelText = message;
[window addSubview:_hud];
[_hud show:YES];
but sometimes _hud not show in window ?
Anyone let me know where im lack here??
Thanks!
Click here!
This works for me .Just use this window.
UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
Or
you can use
self.navigationController.view
to add you HUD.

iPhone: Cannot access NSManagedObjectContext using appDelegate

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]

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