0
votes

Hope someone can help me with this, it has me totally stumped

My application has two view controllers in a tab bar...

//Create two view controllers
UIViewController *vc1 = [[RecordingViewController alloc] init];
UIViewController *vc2 = [[SavedViewController alloc] init];//this is a tableViewController

//Make an array with the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];

//Release the two view controllers since they are now retained by the array
[vc1 release];
[vc2 release];

//Attach the ViewControllers to the tabBar
[tabBarController setViewControllers:viewControllers];

I then have another object (fileHandler) which wants to send an object called capture to vc2 for addition to an NSMutableArray, so from that class I send...

[vc2 addToCapturesArray:capture];

Of course this does not work and I just get the "vc2 undeclared" message from the compiler. How do I let my fileHandler class know about the vc2 instance? Thanks in advance for all your help.


Thanks for the help. It all now compiles, however the method in vc2 is not getting called for some reason. The code now looks like this...

    @interface Rolling_VideoAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;

//Tab bar
UITabBarController *tabBarController;

//Create an ivar so that the app delegate can hold a reference
SavedViewController *_vc2;

} @property (nonatomic, retain) IBOutlet UIWindow *window; //Create a property so we can access vc2 from the outside @property (nonatomic, retain) SavedViewController *vc2;

#import "Rolling_VideoAppDelegate.h"

//Import view controllers
#import "RecordingViewController.h"
#import "SavedViewController.h"

@implementation Rolling_VideoAppDelegate

@synthesize window;
@synthesize vc2 = _vc2;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


//Create an instance of tabBarView controller
tabBarController = [[UITabBarController alloc]init];

//Create two view controllers
UIViewController *vc1 = [[RecordingViewController alloc] init];
UIViewController *vc2 = [[SavedViewController alloc] init];
self.vc2 = _vc2;


//Make an array with the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];

//Release the two view controllers since they are now retained by the array
[vc1 release];
[vc2 release];

//Attach the ViewControllers to the tabBar
[tabBarController setViewControllers:viewControllers];

//Place  the tabBar on the window
[window addSubview:[tabBarController view]];

[self.window makeKeyAndVisible];

return YES;

}

The file handler looks like this...

    Captures *capture = [[Captures alloc]initWithVideoPath:savePath];
NSLog(@"Instance of capture called:\n VideoPath: %@ \n Geo: %@, \n UserNotes: %@", [capture videoPath], [capture location], [capture userNotes]);

// Get a reference to the app delegate, and then access the vc2 property
Rolling_VideoAppDelegate *appDelegate = (Rolling_VideoAppDelegate*)[UIApplication sharedApplication].delegate;

[appDelegate.vc2 addToCapturesArray:capture];

Thanks again for all the help.

Rich

1
Is there any reason that the fileHandler class can't be an instance variable within the second view controller?John Parker
Just so you know, > starts a blockquote environment; indenting by four spaces starts a code environment. daringfireball.net/projects/markdown/syntaxJonathan Sterling

1 Answers

0
votes

Assuming your vc1/vc2 are in the AppDelegate, then what you want is to extend the app delegate something like this

@interface AppDelegate
{
    // Create an ivar so that the app delegate can hold a reference
    VC2Class *_vc2;
}

// Create a property so that you can access if from the outside
@property(nonatomic, retain) VC2Class *vc2;

@end

@implementation AppDelegate

// Map the property to the ivar
@synthesize vc2 = _vc2;

// When you are freeing the app delegate make sure to nil 
// the property to release it
-(void) dealloc
{
    self.vc2 = nil;
    [super dealloc];
}

When you create vc2 in addition to the rest of your code do this:

// Since this is a retain property it will do the retain you need 
// when assigning to self.vc2
self.vc2 = vc2;

Now in your file namanger, do this:

// Get a reference to the app delegate, and then access the vc2 property
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
VC2Class *vc2 = appDelegate.vc2;

Then you can call into it as normal. Make sure to include #import "VC2Class.h" at the top of your file namanger

@end