2

Take the following example..

A single tabBarController toggles between two views, each handled by their own view controller (lets say viewController1, viewController2).

I have a class called Widget that has the property 'name'.

So i have code in view1 that will read the contents of a textfield and set the 'name' property of my widget object.

I want to have code in view2 to read the 'name' property of the widget object and display somewhere.

1. Where would i declare an instance of my widget class? in the tabBarController?

2. If so, how would i access that instance in my other two viewcontrollers?

Thanks in advance!

NoCarrier
  • 2,558
  • 4
  • 33
  • 44

1 Answers1

6

You can put your widget class as a property on the Application Delegate.

@interface youAppDelegate : NSObject <UIApplicationDelegate> {
   Widget *myWidget;
}

@property (readonly) Widget *myWidget;

@end

Then, instantiate it on the message "applicationDidFinishLaunching" of your application's delegate.

Once you've done that, you can access myWidget from anywhere in the project like this:

youAppDelegate *ad = (youAppDelegate*)[UIApplication sharedApplication].delegate;
Widget *w = [ad myWidget];

That code can be perfectly compiled and executed at viewController1, viewController2 or even on the tabBarController.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292