passing data/objects/moc between viewcontrollers - best practice(在视图控制器之间传递数据/对象/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.
在其中一个导航控制器中,我有一个带有 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.
(有很多代码我不确定要呈现哪些位,所以我现在用主要代码段来描述,一定要让我知道你想看到的任何具体代码.)
(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 代码上,将选定的行和这个标记"对象传递给下一个视图,我不确定这是否正确,因为我不确定当我到达第三个视图的主题对象时该怎么做???我迷路了.
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.
给猫剥皮总是不止一种方法,但我应该从什么方法来解决这个问题.我应该将标签"对象向前传递并将其保存在第三个视图中,还是将所选主题传回并将其保存在创建标签"对象的原始视图中?
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];
现在,在具有添加"按钮的控制器中,使其订阅该事件:
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 - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在视图控制器之间传递数据/对象/moc - 最佳实践
基础教程推荐
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01