开发者

iPhone : Getting a value back from views

开发者 https://www.devze.com 2022-12-16 20:10 出处:网络
From the view A I open the View B which contains categories in a uitableview, when I select a category I open the view C which contains subcategories for the category (using the CoreData persistence )

From the view A I open the View B which contains categories in a uitableview, when I select a category I open the view C which contains subcategories for the category (using the CoreData persistence ) in a uitableview, when I select a subcategory I open the view D which contains all my product (using the CoreData persistence ), when I select the the product, how can I set the value into the view A? Avoiding leaks of course...

A(MainWindow)->B(UITableView with category)->C(UITableView with subcategory)->D(UITableView with products) D->C(with the selected product)->B(with the sel开发者_Go百科ected product)->A(with the selected product)

I hope I'm enough understable :D

Thank you


This approach assumes you are using an UINavigationController with a stack of UIViewController instances, each of which having a managed object context instance as a part of a Core Data application.

Set up an ivar and a @property in your application delegate header that holds a value for the selected product (an NSManagedObjectID*, for example):

NSManagedObjectID *selectedProduct;
...
@property (nonatomic, retain) NSManagedObjectID *selectedProduct;

Synthesize this ivar in the implementation, and release it in -dealloc:

@synthesize selectedProduct;
...
- (void) dealloc {
    [selectedProduct release], selectedProduct = nil;
    ...
}

Set up the following macro wherever you keep your constants, or in each view controller header:

#define UIAppDelegate ((MyAppDelegate *)[UIApplication sharedApplication].delegate)

From any view controller, thereafter, you will be able to set and access your selectedProduct value, e.g.:

NSManagedObject *foo;

// set the property
[UIAppDelegate setSelectedProduct:[foo objectID]]; 

// access the property
foo = [self.managedObjectContext objectWithID:[UIAppDelegate selectedProduct]]; 

You don't necessarily have to use an NSManagedObjectID* here. You can use any class you like.


Here are two resources with information related to your question:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/7849-passing-data-back-root-view-controller.html

iPhone SDK - pass data from AppDelegate to a rootviewcontroller


One way would be to pass the pointer to viewA down to viewD through all the other view(controllers ?)

// e.g inside ViewB
...
ViewC *viewC = [[ViewC alloc] initWithFrame: myFrame andMainView: viewA];


// inside ViewD finally
...
[viewA setProductImageWithID: self.selctedProduct.id];

Another way might be making viewA a singleton and instantiating it anywhere you need to use it. There are good examples on Obj-C singletons here on SO.

Take care, marius

ps.: hope I got your problem not completely wrong ;)

0

精彩评论

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

关注公众号