开发者

Accessing object defined in appDelegate

I'm recoding a splitview controller app I did so the table view isn't always on the side. I have my appDelegate, a ViewController, the table view and another view called DetailViewController. I'm declaring a instance of the DetailViewController in my appDelegate.h file

@class SalesMate2ViewController;
@class DetailViewController;
@class Categories;

@interface SalesMate2AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
SalesMate2ViewController *viewController;
DetailViewController *dvc;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet SalesMate2ViewController *viewController;
@property (nonatomic, retain) DetailViewController *dvc;
@end
开发者_开发问答

In the viewController.m I'm creating the actual instance of dvc

dvc = [[DetailViewController alloc] initWithNibName: @"DetailViewController" bundle: nil]];

But when I try to access the dvc instance in the table view it appears I'm dealing with an new object, data doesn't persist or shows null when logged. Everything compiles and runs but code accessing dvc in the table view doesn't work.

In the viewController and table view (h and m files) I'm including the dvc as a @property and @synthesize. Is that what I'm doing wrong? But when I take those out I get warnings saying dvc is undeclared.

I get the feeling I'm dealing with two instances of dvc but I don't know how to fix it. Any hints?

Thanks, Steve


Indeed, in your viewController you are allocing a new instance of dvc, which means anything done in one, won't carry over in the other.

Make an attacher method in yourSalesMate3ViewController and call that from the app delegate. Like so:

SalesMate2ViewController.h

-(void)attachDetailVC :(DetailViewController*) myDvc;

SalesMate2ViewController.m

-(void)attachDetailVC :(DetailViewController*) myDvc
{
   self.dvc = myDvc;
}

And call it from your SalesMate2AppDelegate

self.dvc = [[DetailViewController alloc] initWithNibName: @"DetailViewController" bundle: nil]]
[viewController attachDetailVC:self.dvc];

Now they will both reference the same and either class can manipulate it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜