Referencing AppDelegate Methods - iphone - iphone

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

Related

How to switch between NSObject Class to ViewController

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

iOS switch between two controllers

I have two tab controllers on my delegate and I would like to switch them appear as needed. RootController is displayed as default and when I would like to display secondController from another class I use,
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:appDelegate.secondController.view];
[appDelegate.rootController.view removeFromSuperview];
problem is when I am trying to go back using same method. Crashes due to bad acces exception...
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:appDelegate.rootController.view];
[appDelegate.secondController.view removeFromSuperview];
Any help would be appreciated. Thank you.
You should not remove the rootController From Superview. Remove that line
[appDelegate.rootController.view removeFromSuperview];
and when you want to go back to your rootController just add the following lines:
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.secondController.view removeFromSuperview];

Calling Application delegate file method from main view controller

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

how to call an AppDelegate method from RootViewController without passing a delegate?

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]

How to make a class in one file work with UIWebViewController implemented in another file?

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