How to access the tabBarController from FirstViewController? - iphone

Created a new project from the Tab Bar Application template, how can I access tabBarController (which is declared in AppDelegate) from FirstViewController? I'd like to read some attributes in the tabBarController from the view controller. Thanks!

You can always access the app delegate as follows:
[UIApplication sharedApplication].delegate
So you should be able to get at the tabBarController by doing this:
MyAppDelegate *delegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
Now refer to delegate.tabBarController.
[In the above, MyAppDelegate is whatever name you gave the class of your app delegate.]

Related

Changing Root View Controller to Tab View Controller in core data app (Xcode4)

I'm new to making core data iPhone apps. I created a new core data project in Xcode and attempted to add a tab Bar controller as the root view controller then putting the default tableview controller after the navigation controller in this hierarchy:
->Tab Bar Controller -> Navigation Controller -> TableView Controller -> TableView Controller2 ->details controller
I have implemented an app using navigation and tab bar controllers at the same time before, however for this app I need access to managedObjectModel throughout the app.
I have tried messing around with the app delegate such as didFinishLaunchingWithOptions etc.
I'm always getting messages similar to '
'Terminating app due to uncaught exception
'NSInvalidArgumentException',
reason: '-[UITabBarController
topViewController]: unrecognized selector sent to instance'
This is the code I am using to access the ManagedObjectContext:
AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newCoffee;
newCoffee = [NSEntityDescription
insertNewObjectForEntityForName:#"Coffee"
inManagedObjectContext:context];
And I'm getting this error:
No visible #interface for 'AppDelegate' declares the selector 'managedObjectContext'
I just noticed Xcode says its an ARC issue, how do I access mod with ARC enabled?
Well if I understand your question correct, you want to get access to you NSManagedObjectModel, so you can get acces to your stored data. Am I right?
Well first of all, you should import your Appdelegate in the viewController, where you need to save or load data.
Second of all, you should implement this in your viewDidLoad:
self.context = [self context];
if (self.context == nil)
{
self.context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
with #property (nonatomic, retain) NSManagedObjectContext *context, in the headerfile.
I suggest you pass managed object context from controller to controller. Also if you're initializing some view controller with managed object, you can access MOC via managedObjectContext property of that object.
When saying Managed Object Model you mean Managed Object Context - right?
In both cases: You can access the context and the model at any time via the delegate of the shared application object like so:
[(YourAppDelegateClass *)[UIApplication sharedApplication] managedObjectContext]
But you can also just make a property for each view controller and pass the context down the hierarchy.

Iphone access a property value from AppDelegate

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;

Using 1 AppDelegate for multiple .xib xcode/ipad sdk

Is it a good practice to use one main AppDelegate.h to handle all the ibaction stuff?
is it even possible? if so who does one do this? my IB only lets me link to the associated .m file
firstView.xib only respond to ibaction in firstView.m
I want a button on firstView.xib to respond to ibaction in AppDelegate.m
any thoughts?
No, it's not good practice. But if you really want to...
If you aren't worried about having the same instance of appDelegate holding the IBActions, you can just drag a generic object from the library into your firstView.xib, then change the class to appDelegate. That will allow you to link actions.
You can hook up the actions programmatically: create IBOutlets from the view controller, and in the viewDidLoad method of the view controller, get the [[UIApplication sharedApplication] delegate] and attach it's IBAction methods to the actions of the IBOutlets for the buttons
To access the app delegate from elsewhere in your code, do the following:
#import "AppDelegate.h"
AppDelegate * appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

is it possible to reuse appdelegate in applicationDidFinishLaunching?

I am developing a tab bar application.
I have five tabs in it.
For each tab i have separate navigation controller.
For each tab's table View I want to load data from a web service.
I can do so for one tab by making a separate xmlparser class initializing it with appdelegate then call it in the applicationDidFinishLaunching .
I Can't do so for other tabs . I think that appdelegate conflicts or is it something else is the problem or any other solution .
Create a new appdelegate;
MyAppdelegate *appDelegate = (MyAppdelegate*)[[UIAplication sharedApplication] delegate];
and call applicationDidFinishLaunching.

Why does [[UIApplication sharedApplication] delegate]; return a nil object

I have an iphone application with multiple views and related controllers and xib files. In the controller for the first view that is loaded I am trying to access the delegate for the application but the object that is returned is nil
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
Any ideas why it would be nil?
Because it is not (yet) set. The UIApplication delegate is usually instantiated and set from the MainWindow nib file. If you access it before the nib is loaded completely (e.g. an init method in a controller in the same nib), it is nil.
It can also be because you are attempting to read UIApplication.shared.delegate from code within (or with code triggered from) the AppDelegate's init method.
UIApplication.shared.delegate is not set until after the AppDelegate's init method has returned.