在 Objective-C 中向视图传递数据或从视图传递数据
我需要在应用程序中的不同视图之间传递数据。 不同的视图用于设置在整个应用程序中使用的类的属性。
我以某种方式设法通过在视图周围传递类的副本并在关闭相关视图时设置相应的属性来实现此目的,但我不确定这是实现我需要做的事情的最佳方法。
请参阅下面的伪代码:
- 我有一个“Car”类,其属性为:carColor、carEngineSize、carFuelType ...
- 应用程序中的 MainView 显示 Car 类并使用其方法。这是最终用户与之交互的视图。
- CarSettingsView,可从 MainClass (PopOverFromBarButtonItem:) 访问,打开一个设置弹出窗口,允许用户使用不同的 UITableLayouts 修改 Car 类的属性
Car 类:
@interface Car : NSObject
{
id delegate;
NSString *carModel;
NSString *carColor;
BOOL turboCharged;
int carEngineSize;
NSString *carFuelType;
}
@property [...]
MainView 实现 myCar 在整个应用程序中使用的类。
CarSettingsView:
实现一个 dummyCar 汽车。
@class Car;
@interface SettingsPopOver : UITableViewController <UIPopoverControllerDelegate>
{
NSMutableArray *tableContentsArray;
UINavigationController *navigationController;
SetParametersView *paramView;
Car *dummyCar;
}
从 MainView 调用 CarSettingsView 时,我会传递 myCar :
- (IBAction)btnSettings:(id)sender {
settings = [[SettingsPopOver alloc]init];
[settings setDummyCar:myCar]; //Pass over myCar from the MainView
UIPopoverController *aPopOver = [[UIPopoverController alloc]initWithContentViewController:settings];
//self->popOverController = aPopOver;
popOverController = aPopOver;
popOverController.delegate = self;
[popOverController setPopoverContentSize:CGSizeMake(300, 400)];
[popOverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
然后直接在设置视图中的 dummyCar 上执行任何属性更改,然后将 dummyCar 复制回 myCar< /em> 在关闭设置视图时:
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
myCar = [settings dummyCar];
}
这对我来说没问题。但我不确定这是正确的方法,而且当嵌套多个设置视图并且对象需要向下传递几层然后返回到初始对象所在的主视图时,它也会变得有点尴尬。
很高兴听到对此的评论和/或在线链接,这些链接将使我走向正确的方向。
I need to pass data between different views in my application.
The different views are used to set properties of a class that is used throughout the application.
I have somehow managed to achieve this by passing copies of the class around the views and setting the corresponding properties when the view in question is closed, but I am not sure this is the best way to achieve what I need to do.
See below my pseudo code:
- I have a 'Car' class, with properties: carColor, carEngineSize, carFuelType ...
- MainView in the application displays the Car class and uses its methods. This is the view end users interact with.
- CarSettingsView, accessible from the MainClass (PopOverFromBarButtonItem:) opens a settings pop over that allows the user to modify the properties of the Car class using different UITableLayouts
Car Class:
@interface Car : NSObject
{
id delegate;
NSString *carModel;
NSString *carColor;
BOOL turboCharged;
int carEngineSize;
NSString *carFuelType;
}
@property [...]
MainView implements a myCar class that is used throughout the application.
CarSettingsView:
Implements a dummyCar Car.
@class Car;
@interface SettingsPopOver : UITableViewController <UIPopoverControllerDelegate>
{
NSMutableArray *tableContentsArray;
UINavigationController *navigationController;
SetParametersView *paramView;
Car *dummyCar;
}
When Calling CarSettingsView from the MainView, I pass over myCar :
- (IBAction)btnSettings:(id)sender {
settings = [[SettingsPopOver alloc]init];
[settings setDummyCar:myCar]; //Pass over myCar from the MainView
UIPopoverController *aPopOver = [[UIPopoverController alloc]initWithContentViewController:settings];
//self->popOverController = aPopOver;
popOverController = aPopOver;
popOverController.delegate = self;
[popOverController setPopoverContentSize:CGSizeMake(300, 400)];
[popOverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
I then perform any property changes directly on dummyCar in the settings view, then copy dummyCar back into myCar at the dismissal of the settings view:
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
myCar = [settings dummyCar];
}
That works OK for me. But I am not sure it is the right way, also it becomes a bit awkward when multiple settings views are nested and the object needs to be passed down several layers and then back up to the main view where the initial object resides.
Happy to hear comments on this, and/or online links that would put me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有读完你的整个问题。 “但我不确定这是正确的方法,而且当嵌套多个设置视图并且对象需要向下传递几层然后返回到初始对象所在的主视图时,它也会变得有点尴尬。”
看起来您希望在多个视图控制器或多个类之间共享价值......那么单例是一个不错的选择。
http://cocoawithlove.com/2008/11/singletons-appdelegates -and-top-level.html Matt Gallagher 撰写的这篇文章对此进行了解释。
I haven't read your entire question. " But I am not sure it is the right way, also it becomes a bit awkward when multiple settings views are nested and the object needs to be passed down several layers and then back up to the main view where the initial object resides."
Looks like you are looking to share value across multiple view controllers or multiple classes... then Singletons are way to go.
http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html this article written by Matt Gallagher explains about it.
最后获取设置控制器的 dummyCar 属性并不是一个好主意。
但是有两种好的做法与您正在做的事情非常接近:
想法1 - 将 dummyCar 作为“分配”或“保留”属性。这样,您将能够直接从设置控制器更改汽车对象属性。最后不需要在主调用者视图中执行此操作。
这将导致一种类似于您在设置 iDevice 设置时看到的方法:无需要求用户保存。任何更改都会直接考虑在内。
想法 2 - 将 dummyCar 设置为“复制”属性(最佳)。这样原车就不会因您的更改而改变。执行您需要的操作,并在解雇时对主调用者调用的方法中,传递您想要替换原始汽车的修改后的汽车对象。然后进行替换。
实施 NSCopying 协议 my Car
和
Getting the dummyCar property for the settings controller at the end is not a good idea.
But two good ways of doing are very close of what you are doing :
Idea 1 - Make dummyCar as an "assign" or "retain" property. Doing that way, you will be able to change the car object properties directly from the settings controller. No need to do it in the main caller view at the end.
That will lead to a way of doing that is like the one you can see when setting your iDevice settings : no need to ask the user to save. Any change is directly taken into account.
Idea 2 - Make the dummyCar as a "copy" property (best). That way the original car is not modified by your changes. Do what you need, and in the method you call on the main caller at dismissal time, pass the modified car object you want to replace the original car. Then do that replace.
Implement NSCopying protocol my Car
and