how to call an AppDelegate method from RootViewController without passing a delegate?
Just wondering whether there is a way to do this? (or do I need to create a delegate object in the RootViewController to hold a reference to the AppDelegate)
[[[UIApplication sharedApplication] delegate] someMethod];
Works like a charm!
You can get access to the app delegate from any controller using
MyDelegate* aDelegate = (MyDelegate *)[[UIApplication sharedApplication] delegate];
This happens so frequently, that I always add a method to MyCustomAppDelegate to do this for me (so I don't have a lot of casting in my code.
#implementation MyCustomAppDelegate ()
- (MyCustomAppDelegate *)appDelegate {
return (MyCustomAppDelegate *)[[UIApplication sharedApplication] delegate];
}
#end
now anywhere i can call
[MyCustomAppDelgate appDelegate]
Related
I'm having to class one which extends NSObject and second the view controller.
What is my problem is that I want to go to NSObject to view controller that loads my xib file of that view controller.
I have used the traditional, with no success thus far.
Suppose NSObject is MagentoLogin.m and I want to go to viewController, how can I achieve this?
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController presentViewController:myNewViewController animated:YES completion:nil];
try this hope it will help
The situation I am running in is that, I have a login Button which is having NSURLConnection method in the main view controller and when button is pressed it will be active and and XML file will be downloaded. Now to parse that XML file the method which is parsing is in application delegate file.
Now I want to know that how can I call that function from within the login button. And also that from that parsed file I have to check a value that if it is more than 1 or less than one. So that the action would be taken that to which view it will be transitioned.
any help will be appreciated.
If the method to do your parsing lives in the application delegate, then getting a hook to it can be as easy as:
MyFineApplicationDelegate * appDelegate =
(MyFineApplicationDelegate *)[[UIApplication sharedApplication] delegate];
And then you can call [appDelegate parseMyXMLData: myXMLData];
Makes sense?
Try This..
In .h
#import "Appdelegatefile.h"
Appdelegatefile * appDelegate;
IN .m
{
appDelegate =(Appdelegatefile *)[[UIApplication sharedApplication] delegate];
[appDelegate //here you can call method of delegate file ];
}
I need to share data from a "SampleAppdelegate" to two view controller "ContactViewController.m" and "DrinksViewController.m".
In SampleAppdelegate:
#interface SampleAppdelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate , UINavigationBarDelegate>
So I've included below code in respective view controller.
In ContactViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
SampleAppDelegate *appDelegate = (SampleAppDelegate *)[[UIApplication sharedApplication] delegate];
contactDatabaseObject = (ContactDatabaseObject *)[appDelegate.contactDetails objectAtIndex:0];
NSLog(#"%#", contactDatabaseObject.contactName);
[appDelegate release];
}
In DrinksViewController.m
(void)viewDidLoad
{
[super viewDidLoad];
SampleAppDelegate *appDelegate = (SampleAppDelegate *)[[UIApplication sharedApplication] delegate];
drinksDatabaseObject = (DrinksDatabaseObject *)[appDelegate.drinkDetails objectAtIndex:0];
NSLog(#"%#", drinksDatabaseObject.drinkName);
[appDelegate release];
}
The problem is "sharing application delegate is is both of the code not working at the same time. ( that is, if I comment above in a view controller then other is working fine as as expected).
Actually i'm using a tab-bar with two item as above viewControllers. So loading of the first controller happens fine. But when i click the second view controller the iphone-simulator is terminated with any warning. ( And at the same time the code works normal if I comment it in any one of the view controller). Please help me to resolve above issues or suggest a method to accomplish above feature.
Thanks in advance !
You should read ...
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html
You're not owner of appDelegate, no need to release it.
P.S. If you're asking for help and writing something about warning, you should always copy warning here to shed more light on your problem.
You should not be releasing the appDelegate.
So I have a UIWebView implemented in one .m file and a UITableView implemented in another. When a user clicks UITableView element, the UIWebView appears. The problem is that I have to set UIWebView's content (local text and images) depending on the UITableView's row number. I know how to do this, but this, of course must be implemented with the UITableView but it also needs UIWebView.
Just importing the UIWebView implementation does'n work.
Thanks in advance!
see this link to for setting delegates here
In didSelectRowAtIndexpath
NSString *name=#"content of table";
myAppDelegate *appDelegate = (myAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate setCurrentTitle:name];
PictureWebViewController *webview = [[PictureWebViewController alloc] initWithNibName:#"PictureWebViewController" bundle:nil];
[self.navigationController pushViewController:email animated:YES];
the PictureWebViewController is class of your webview .
In that class in viewdidload method get the title as
myAppDelegate *appDelegate = (myAppDelegate *) [[UIApplication sharedApplication] delegate];
NSString *name= [appDelegate getCurrentTitle];
Use the name in this class to display the content in webview.
All the Best
I have an AppDelegate which has 3 views. I add all three
[window addSubview:gameViewController.view];
[window addSubview:viewSettings.view];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
In the app delegate, i have some methodes for swapping views by calling
[window bringSubviewToFront:gameViewController.view];
When i am inside viewController, I use
pinkAppDelegate *appDelegate= (pinkAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate switchToSettings];
to switch my subviews...so far so good.
BUT, when I'm in my viewSetting UIViewController, and do the same appDelegate call, it chocks, like it doesn't understand how to call the appDelegate method.
I've got all my views hooked in my mainwindow xib, but can't figure out why i can't traverse the methods in the main appdelegate
Inside viewController, you're setting up a local variable called appDelegate that points at your app delegate. That's what this line does:
pinkAppDelegate *appDelegate= (pinkAppDelegate *)[[UIApplication sharedApplication] delegate];
This variable is local to viewController, so you can't use it in the settings view controller. You need to set up another variable there.
Alternatively, use this nice #define throughout your app. (You'll need to put it in a .h header file that you include in every file in your project.)
#define myAppDelegate (MyAppDelegate *)[[UIApplication sharedApplication] delegate]
Then you can do the following anywhere:
[myAppDelegate doSomething];
For reference there is still a typo in the above, also I have another addition which helps avoid warnings of having to cast every time. if you use:
#import "MyAppDelegate.h"
#define myAppDelegate (MyAppDelegate *) [[UIApplication sharedApplication] delegate]
I put the above in a constants.h and then can use
[myAppDelegate doSomething];
anywhere I import constants.h
Jane's answer had a typo in it.
Use the following line in a header file (e.g. defines.h) that you import in every class:
#define myAppDelegate [[UIApplication sharedApplication] delegate]
You could also use the line in a prefix header file - this is an easy way of getting access to this everywhere.
then in any method you can use any of the following:
[myAppDelegate doSomething];
myAppDelegate.property=value;
myAppDelegate.childClassOfMyAppDelegate.property=value;
[myAppDelegate.ChildOfMyAppDelegate doSomething];