Iphone access a property value from AppDelegate - iphone

How to access a property value of AppDelegate class from someView Controller without creating reference of the delegate in view controller?

I'm not quite sure what you mean - there are multiple ways to get information from your application delegate into a view controller, and the phrase "without creating reference of the delegate" is unclear. Your options basically are:
Reference the application delegate, casting as appropriate. You would write code in your view controller class like: id propertyValue = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] myProperty];
Pass the property in when creating the view controller. This requires the view controller to have a #property declared and #synthesized for use, then you would have the app delegate just set the property on the view controller instance.
Neither of these options require that you retain a copy of your app's delegate as a #property, but the first does reference the delegate once.

[UIApplication sharedApplication].delegate
You'll also need to include the app delegate header file in your view controller and possibly typecast the delegate from id to your actual app delegate class.
#include "MyAppDelegate.h"
((MyAppDelegate *)[UIApplication sharedApplication].delegate).myProperty;

[UIApplication sharedApplication].delegate;

Related

inject managedObjectContext into initial viewController?

I am setting up a Core Data model (UIManagedDocument) in my AppDelegate and was curious about how I might inject a reference to the NSManagedObjectContext into my (storyboards) Initial viewController.
I could access it directly in the viewControllers viewDidLoad using [[[UIApplication sharedApplication] delegate] sampleDatabase] managedObjectContext] and then pass it onto further controllers using prepareForSegue.
I was just wondering if there is an initial segue (or mechanism) that would allow me to pass (or inject) the managedObjectContext into that initial viewController without having to access it using the appDelegates shared instance once inside the controller?
When you use a storyboard, the initial view controller is set as the rootViewController property of your delegate just before application:didFinishLaunchingWithOptions: is called.
You should be able to use a cast on that reference to set a property, assuming your Core Data structure is set up by then.
Typically you would just have a property on the view controller that is a NSManagedObjectContext and just set that property right after you initialize the viewController in your appDelegate. myViewController.managedObjectContext = self.managedObjectContext

How to access one UIViewControllers properties from another UIViewController?

I have one single MainViewController which has of course it's one main UIView. I have this main view comprised of many different subviews.
Some of these subviews have their very own ViewController.
Lets say the MAIN view (whose delegate is primarily MainViewController) has a container which loads another UIView that uses a separate UIViewController- SecondaryViewController as the delegate for most it's actions.
This container view is of course loaded in MainViewController via
MyContainerViewController *myContainerController =
[[MyContainerViewController alloc] ...];
[self addSubView: myContainerController.view];
the controller for myContainerController.view though is MyContainerViewController. How inside this controller do I access MainViewController properties? Specifically I need to access MainViewController's - self.navigationController property to push a new ViewController? :)
Does this make any sense? I assume there's going to be casting of some sort involved since it seems I need to somehow retain a reference to MainViewController inside SecondaryViewController?
It doesn't make sense to push a new ViewController from the SecondaryViewController in the MainViewController.
This screws up the design of the code. A child object will access its parents method to call a method. By other words: bad code.
Make a delegate call from the SecondaryViewController to the MainViewController that it state has changed. Then the MainViewController can decide to do with the call and the SecondaryViewController will not know anything about the implementation of the MainViewController.
So:
Make a protocol in SecondaryViewController.
Let the MainViewController be SecondaryViewController's delegate.
Implement the delegate method in MainViewController which pushes the new ViewController.
Expose the desired sub-view controllers as properties of the view controller that contains them.
Expose your root view controller(s) as properties of your app delegate, and synthesize them also.
When you want to access a different UIViewController, first obtain a reference to your appDelegate:
MyAppDelegate* myAppDelegate = [[UIApplication sharedApplication] delegate];
Then just use your chain of properties to get access to your desired view controller.
SubSubViewController* ssvc = myAppDelegate.rootViewController.subViewController.subSubViewController;
Some folks frown upon the use of the sharedApplication class method to obtain the reference to a delegate, but I've used it in multiple apps and not suffered for it (yet). The alternative is to pipe your appDelegate through your hierarchy of viewControllers.

accessing view controller's view

I am inside a class on a view-based app, one that was creating with one view controller.
WHen I am inside the view controller I can access its view using self.view, but how do I access the same view if I am inside a class?
[[UIApplication sharedApplication] delegate]... //??? what do I put here?
thanks
Assuming that the view controller is a member of your application delegate. you can access it like this: ("YourAppDelegate" should be replaced with the actual type name of your application delegate)
( ( YourAppDelegate *) [ [ UIApplication sharedApplication ] delegate ] ).viewController.view;
If you're trying to give a singleton a reference to your main viewController, a better solution might be to get your MyAppDelegate to set a viewController property on your singleton at launch:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[MySingletonClass sharedSingleton].viewController = self.viewController;
}
If you plan on re-using a Singleton like this in multiple projects, it often makes more sense to set these sort of properties from your AppDelegate.
A subview really shouldn't be manipulating a parent view, each class should manage it's own view.
Anyway, if your class is a subclass of UIView and is a subview of the viewController, you can get the viewController's view by accessing the superview property to get your parent view.

how to get to appdelegate from viewcontrollers' value?

i know how to access appdelegate's value inside Viewcontroller like
YourDelegate *appDelegate = (YourDelegate *)[[UIApplication sharedApplication] delegate];
but i want simple method like this when i want to get value from viewcontroller to appdelegate(reverse order).....? any help...?
suppose if i have one method in appdelate. i want to get data value from view controller page,i want to use it in appdelegte.m file.......?
To address this question more generally...If you want to do anything with an object – send it a message (call a method on an object), access some property of an object, pass the object as a parameter to some other method – you need a reference to that object.
That means that, in your case, your AppDelegate needs a reference to the view controller you want to access some property of. If the view controller is allocated and initialized in your app delegate, this is as simple as storing a reference to said view controller in your delegate until you need to use it (using an instance variable or whatever). If it wasn't, then you need to do something else to get your app delegate a reference to the view controller – the steps to do this would depend on where and how the view controller was created. Without more specific details, I can't help you with those steps.
Model-View-Controller (MVC) Sidenote:
If you are following MVC design practices, a view controller (or any other controller class) is not the object that should be storing your state information or other application data. That job is supposed to be performed by a model object.
Make the method be a class method (declared with + (void) MyMethod: (int)myParameter) and call it from your app delegate like this: [MyOtherViewController MyMethod: myParameter].
This to call checkAppTheme method from AppDelegate.m :
[(AppDelegate *)[[UIApplication sharedApplication] delegate] checkAppTheme];
Don't miss to change (checkAppTheme) to your method in your AppDelegate.m
Good luck!

How can I outsource view-change-operations from my view controller upon accelerations?

I have an view controller class. I want to do some things in my view when the accelerometer detects movement and calls the accelerometer:didAccelerate: method in the delegate object.
That delegate object is the problem here in my brain. Currently, my brain is freezed and I don't get it what would be better. Let me know what you think!
Solution 1)
In my view controller class I conform to the UIAccelerometerDelegate protocol, and implement that accelerometer:didAccelerate: method.
In the -applicationDidFinishLaunching: method of my AppDelegate class I set that view controller object up as the delegate for receiving method calls upon accelerations. I think that's not really good.
Solution 2)
I create a blank new object called AccelerationDelegate, conform to that UIAccelerometerDelegate protocol, implement that accelerometer:didAccelerate: method and in the -applicationDidFinishLaunching: method of my AppDelegate class I set that view controller object up as the delegate for receiving method calls upon accelerations.
But for solution 2 my brain got stuck a little bit! How would I access the view objects from my view controller inside that object?
The problem here is, that I have more than one view controller around. I use a tab bar controller to switch between them.
Any suggestions how I could get that right?
I agree that the second method is better. Are you looking to access just the currently selected tab view, or just a specific view in your app.
In any case, what I would do is to set up properties for your UITabViewController in your UIApplicationDelegate so that you can access it from the delegate (you can get the app delegate by calling [[UIApplication sharedApplication] delegate]). For example:
YourApplicationDelegate *appDelegate = (YourApplicationDelegate *)[[UIApplication sharedApplication] delegate];
FirstUIViewController *firstViewController = appDelegate.firstViewController;
[firstViewController doStuff];
where firstViewController is a property on your delegate.
If your acceleration is specific to one view controller, then it makes sense to have the view controller receive the information necessary to alter its own subviews. However, it might be better to set your view controller to be the delegate when the view appears, and set the delegate to null when it disappears. (Specifically, - (void) viewWillAppear: and - (void) viewWillDisappear:)