Calling Application delegate file method from main view controller - iphone

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

Related

load data into uipickerview with NSMutableArray located in app delegate

basically i have parsed some data from XML into a NSMutableArray that is shared in the appDelegate.
in my secondViewController i have a uiPickerView that i am wanting to load the details of the array into it.
My question is... how?
i have briefly worked with uiPickerView's before and had to load the data in first to assign to the uiPickerView like so:
titleDB = [[NSMutableArray alloc] init];
[titleDB addObject:#"MR"];
[titleDB addObject:#"Mrs"];
[titleDB addObject:#"Ms"];
[titleDB addObject:#"Miss"];
[titleDB addObject:#"DR"];
[titlePickerView selectRow:1 inComponent:0 animated:YES];
but since the data is coming from the appDelegate i don't know how i should load it into the uiPickerview, is it something to do with the datasource?
I'm asking to throw code at me I'm just asking for the best way to do it.
Any help on this would be great
Thanks
Jonn4y
This is a common pattern. You will want to access UIApplication's sharedApplication instance. So assuming your appDelegate class is named YourAppDelegate, the array ivar in YourAppDelegate and viewController is titleDB then you could do this in your viewController's viewDidLoad method
YourAppDelegate *appDelegate=(YourAppDelegate *)[[UIApplication sharedApplication] delegate];
// assuming you are using #property and #synthesize for your ivars
self.titleDB=appDelegate.titleDB;
Good luck
Basically, you want to access the appDelegate object from random places in code. This is not an unusual request. Remember that Objective C is a superset of the C language. And as such, you can use a global variable. What more natural variable would there be in a Cocoa program than the app delegate, for the reasons stated above. So, in your appDelegate .h file, add:
<MyAppDelegateClass> * appDelegate;
Substitute MyAppDelegateClass for the name of your appDelegate class name. Then just include your appDelegate's .h file anywhere you want to use the appDelegate variable, and just use (in your example):
[appDelegate titleDB]
or create a local iVar:
NSMutableArray * titleDB = [appDelegate titleDBData];
Then in your app delegate method didFinishLaunchingWithOptions, add the following line:
appDelegate = self;

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]

Call parent function in xcode iphone development

We have 2 files 'MainViewController' and 'View01'
The first file addSubView the second one.
In the first file we have a function named displayView.
I want to be able to call this function from the View01 file is the following code correct
part of View01 file
#import "MainViewController.h"
- (IBAction) changeView:id(sender){
MainViewControlle *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate displayView:2];
}
is this the correct way to do it?
at the moment I use the NSNotificationCenter to send activate the functions :)
You should try using delegate methods :-)
It works like that :
// Delegate
#protocol MyViewDelegate <NSObject>
- (void)myViewAskedForSomethingWithOrWithoutParameters;
#end
In your view you must have this parameter :
id<MyViewDelegate> delegate;
And then in your Controller you must implement the delegate
#interface MainViewController : UIViewController <MyViewDelegate> {
}
In your implementation you need to add a :
myView.delegate = self;
And finally in your view when you need to call the function, just do :
[ delegate myViewAskedForSomethingWithOrWithoutParameters ];
Good Luck !
If I'm not mistaken, your code shouldn't work. Did you try it your self?
[[UIApplication sharedApplication].delegate]
will return your AppDelegate, called something like MyAppDelegate and not MainViewController. However, depending on the template you used or created, your AppDelegate might contain a MainViewController-property, most likely called viewController so you could use
[appDelegate.viewController displayView:2];
This is the quick way to do it. For a more tidy way, see Vinzius' answer.

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

Referencing AppDelegate Methods - 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];