在视图控制器之间传递数据/对象/ moc - 最佳实践 [英] passing data/objects/moc between viewcontrollers - best practice

查看:136
本文介绍了在视图控制器之间传递数据/对象/ moc - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我怀疑是非常常见的,我发现了各种想法对其他类似的问题,包括设置IBOutlets,传递NSmanagedobjects作为属性,只是使用一个视图控制器,但换出视图,

I have a scenario that i suspect is very common, i've found various ideas in responses to other similar questions including setting up IBOutlets, passing NSmanagedobjects as properties and just using one view controller but swapping out the views but I'm uncertain as to which would be the best idea to implement for my solution.

我有一个有典型模型的iOS / iphone应用程序。 UITabBarController包含多个UINavigationControllers。

I have an iOS/iphone app that has a typical model. UITabBarController containing multiple UINavigationControllers.

在其中一个导航控制器,我有一个viewcontroller与tableView。此tableView是从获取的核心填充数据实体称为标签。当我使用UIBarButtonItem(Add)设置tableView时,添加按钮的行为如下:我需要它为该实体创建一个新的标签对象,并用一些已知数据填充该对象,然后提示用户钻取向下3级分类(区域 - >区域 - >主题)到达主题对象。然后我需要添加'主题'对象到原来新添加的标签对象的关系(3视图进一步向下堆栈)。 - 希望清楚。

In one of the navigation controllers i have a viewcontroller with a tableView in it. This tableView is populated from fetched core Data entity is called 'tags'. When I set the tableView up with a UIBarButtonItem (Add) the add button needs to behave as follows: I need it to create a new 'tag' object for that entity and populate the object with some known data and then prompt the user to drill down 3 levels of classification (Region -> area -> topic) to reach a topic object. i then need to add that 'topic' object to a relationship of the original newly added 'tag' object (3 views further down the stack). - hope thats clear.

(有很多代码我不知道哪些位,所以我现在描述的主要片段,一切手段let我知道你想看到的任何特定代码。)

(there's lots of code I'm not sure which bits to present so i'm describing for now with the main snippet, by all means let me know any specific code you'd like to see.)

- (void)addTag {    

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newManagedObject setValue:@"(untitled)" forKey:@"tagID"];

NSError *error;
if (![context save:&error])
    NSLog(@"Error saving entity: %@", [error localizedDescription]);


ChooseRegion *aView = [[[ChooseRegion alloc] init] autorelease];
aView.theTag = newManagedObject;

[self.navigationController pushViewController:aView animated:YES];

}

你可以看到我已经创建了对象,的属性值,保存它并将对象传递给下一个视图(chooseRegion),这是3个视图中的第一个视图。在每个didSelectRowAtIndexPath代码传递选定的行和这个'tag'对象到下一个视图,我不知道这是否是正确的,因为我不知道当我得到第三个视图上的主题对象该怎么办? ??我迷路了。

You can see I've created the object, set one of it's properties a value, saved it and passed the object to the next view (chooseRegion) which is the first of 3 views. on each the didSelectRowAtIndexPath code passes the selected row and this 'tag' object to the next view, i'm not sure whether this is right because i'm not sure what to do when i get to the topic object on the 3rd view??? i'm lost.

总有不止一种方法可以使一只猫皮肤化,但是我应该采取什么方法来解决这个问题。我应该向前传递标签对象并保存在第3视图或传回所选主题并将其保存在创建标签对象的原始视图?

Always more than one way to skin a cat but what approach should i be tackling this from. Should I be passing the 'tag' object forward and save it at the 3rd view or pass back the selected topic and save it at the original view which created the 'tag' object?

提前非常感谢。

推荐答案

我会使用通知。通知是一种简单的方法来分离应用程序的某些部分,但仍然需要它们一起工作。

I would use notifications for this. Notifications are a simple way to decouple parts of the application, but still have them work together.

在您的情况下,用户在第三个视图中选择标记对象,我会像这样发送通知:

In your case, the moment the user selects the tag object in the 3rd view, I would send a notification as such:

[[NSNotificationCenter defaultCenter] postNotificationName:@"tagSelected" object:myTag];

现在,在具有添加按钮的控制器中,使其订阅该事件: / p>

Now, in the controller that has the "add" button, make it subscribe to that event:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTagSelected:) name:@"tagSelected" object:nil];

确保您实现了handleTagSelected:方法,并且在该方法中,您可以获取标记对象并关闭您打开的展开视图:

Make sure you implement the handleTagSelected: method, and in that method you can get the tag object and close the drilldown view that you have open:

- (void)handleTagSelected:(NSNotification *)notification {
    Tag *mytag = (Tag *)notification.object;
    [self dismissModalViewControllerAnimated:YES];
}

然后你可以用标签做任何你想要的。

Then you can do whatever you want with the tag.

这篇关于在视图控制器之间传递数据/对象/ moc - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆