Отправить объект в контроллер UITableView - PullRequest
0 голосов
/ 10 декабря 2010

Надеюсь, что кто-то может помочь мне с этим, это поставило меня в тупик

Мое приложение имеет два контроллера представления в панели вкладок ...

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

У меня тогда есть другой объект (fileHandler), который хочет отправить объект с именем capture в vc2 для добавления к NSMutableArray, поэтому из этого класса я отправляю ...

[vc2 addToCapturesArray:capture];

Конечно, это не работаети я просто получаю сообщение "vc2 undeclared" от компилятора.Как сообщить классу fileHandler об экземпляре vc2?Заранее спасибо за вашу помощь.


Спасибо за помощь.Теперь все это компилируется, однако метод в vc2 по какой-то причине не вызывается.Код теперь выглядит следующим образом ...

    @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;// Создаем свойство, чтобы мы могли обращаться к vc2 извне @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;

}

Обработчик файла выглядит следующим образом ...

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

Еще раз спасибо за помощь.

Rich

1 Ответ

0 голосов
/ 10 декабря 2010

Если ваш vc1 / vc2 находится в AppDelegate, тогда вам нужно расширить делегирование приложения примерно так:

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

Когда вы создаете vc2 в дополнение к остальному коду, сделайте следующее:

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

Теперь в вашем файле namanger, сделайте следующее:

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

Тогда вы можете звонить как обычно. Обязательно включите #import "VC2Class.h" в верхней части вашего файла namanger

@ конец

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...