开发者

Sharing data across a Cocoa application

I have searched for how to correctly do this, but I don't believe I have found my answer. This is my setup:

NSWindowController loads in 2 different NSViewControllers (only one is displayed at a time). When I drop a file onto the NSViewController's view that is loaded, I want to save the path of that file. I can get all of this, but what I want to be able to do now is when I swap to my other NSViewController, I want to pass on this file path to the new NSViewController.

The only solution I have come up with is to post a notification from my NSViewController to the NSWindowController and then initialize the 2nd NSViewController with this data, but it seems rather convoluted. Is there a way that I can just save the data somewhere as a global entity and then access it later from my 2nd NSViewController?

NOTE: The NSViewControllers are being released after they are swapped with the other.

Any insight would be appreciated.

UPDATE: I'm just going to change the application so that both view controllers are in memory at all times. This way I can do as others have suggested and use KVO 开发者_如何学编程or other methods.


View controllers are meant for controlling views of model objects. So each of your view controllers must be getting their data from some underlying model object which presumably in your case is the same one. If you don't have that, I suggest you re-engineer your application to the MVC pattern.

When you create your view controllers you can set the representedObject property and store the file name as one of its properties. If your model object is KVO compliant you can even have each view controller observe the file name property of the model object and react to changes when they happen.


You could save it in a global variable/singleton somewhere, but that's not the right solution either.

One option is to give your first view controller a pointer to the second one, perhaps as a delegate (so that your first controller isn't unnecessarily dependent on the second one). Then it can just send an appropriate message to its delegate. Note that the window controller could also be the delegate if that's more appropriate for whatever reason.

Another option is to have the second view controller listen for the notification instead of the window controller. If the second view controller doesn't (or might not) exist, then your current solution of letting the window controller handle the notification is pretty reasonable.


How are you doing the swapping exactly? Say you had a Navigation Controller that's controlling which view gets pushed to the top. Why don't you have a member of the second view controller that holds the file path?

That way you can set this value on the view controller and push that onto the navigation stack to swap.

SecondViewController *childController = [[SecondViewController alloc] initWithStyle:UITableViewStyleGrouped];
childController.title = @"Second View";
childController.filePath = myFilePath; //this is where you set the member
[self.navigationController pushViewController:childController 
                                     animated:YES];
[childController release];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜