这是应用程序架构的更高级主题(如何传递数据)。
- 肮脏的快速解决方案:
NSNotification
与遗忘一起发布representedObject
:
所有 NSViewControllers 都有一个很好的 id 类型属性,称为representedObject
. 这是将数据传递到NSViewController
. 将您的标签绑定到此属性。对于这个简单的例子,我们将设置representedObject
一些 NSString instance
。您也可以使用复杂的对象结构。有人可以在评论中解释为什么故事板停止显示representedObject
(快速输入安全?)
接下来我们添加通知观察者并在处理程序中设置表示的对象。
@implementation RightViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserverForName:@"SelectionDidChange" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
//[note object] contains our NSString instance
[self setRepresentedObject:[note object]];
}];
}
@end
左视图控制器及其表:一旦选择更改,我们将使用我们的字符串发布通知。
@interface RightViewController () <NSTableViewDelegate, NSTableViewDataSource>
@end
@implementation RightViewController
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return [[self names] count];
}
- (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
{
return [self names][row];
}
- (NSArray<NSString *>*)names
{
return @[@"Cony", @"Brown", @"James", @"Mark", @"Kris"];
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
NSTableView *tableView = [notification object];
NSInteger selectedRow = [tableView selectedRow];
if (selectedRow >= 0) {
NSString *name = [self names][selectedRow];
if (name) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"SelectionDidChange" object:name];
}
}
}
PS:不要忘记在情节提要中挂钩 tableview 数据源和委托
为什么这个解决方案很脏?因为一旦您的应用程序增长,您最终将陷入通知地狱。还将控制器视为数据所有者?我更喜欢窗口控制器/appdelegate 作为模型所有者。
结果:
- AppDelegate 作为模型所有者。
我们的左视图控制器将从AppDelegate
. 控制数据流和设置数据很重要AppDelegate
(不是视图控制器询问AppDelegate
它的表格内容,因为您最终会陷入数据同步混乱)。我们可以再次使用representedObject
. 一旦设置好,我们重新加载我们的表(有更高级的解决方案,如 NSArrayController 和绑定)。不要忘记在情节提要中挂钩 tableView。我们还修改了tableview的委托方法the tableViewSelectionDidChange
来修改我们的模型对象(AppDelegate.selectedName)
#import "LeftViewController.h"
#import "AppDelegate.h"
@interface LeftViewController () <NSTableViewDelegate, NSTableViewDataSource>
@property (weak) IBOutlet NSTableView *tableView;
@end
@implementation LeftViewController
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return [[self representedObject] count];
}
- (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
{
return [self representedObject][row];
}
- (void)setRepresentedObject:(id)representedObject
{
[super setRepresentedObject:representedObject];
//we need to reload table contents once
[[self tableView] reloadData];
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
NSTableView *tableView = [notification object];
NSInteger selectedRow = [tableView selectedRow];
if (selectedRow >= 0) {
NSString *name = [self representedObject][selectedRow];
[(AppDelegate *)[NSApp delegate] setSelectedName:name];
} else {
[(AppDelegate *)[NSApp delegate] setSelectedName:nil];
}
}
在RightViewController
我们删除所有代码。为什么?因为我们将使用绑定 AppDelegate.selectedName <-->
RightViewController.representedObject
@implementation RightViewController
@end
最后AppDelegate
。它需要暴露一些属性。有趣的是我如何得到我所有的控制器?一种方法(最好的)是实例化我们自己的窗口控制器并将其作为属性记住。另一种方法是向 NSApp 询问它的窗口(这里要小心多窗口应用程序)。从那里我们只需询问 contentViewController 并遍历 childViewControllers。一旦我们有了控制器,我们只需设置/绑定表示的对象。
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (nonatomic) NSString *selectedName;
@property (nonatomic) NSMutableArray <NSString *>*names;
@end
#import "AppDelegate.h"
#import "RightViewController.h"
#import "LeftViewController.h"
@interface AppDelegate () {
}
@property (weak, nonatomic) RightViewController *rightSplitViewController;
@property (weak, nonatomic) LeftViewController *leftSplitViewController;
@property (strong, nonatomic) NSWindowController *windowController;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
_names = [@[@"Cony", @"Brown", @"James", @"Mark", @"Kris"] mutableCopy];
_selectedName = nil;
NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main"
bundle:[NSBundle mainBundle]];
NSWindowController *windowController = [storyboard instantiateControllerWithIdentifier:@"windowWC"];
[self setWindowController:windowController];
[[self windowController] showWindow:nil];
[[self leftSplitViewController] setRepresentedObject:[self names]];
[[self rightSplitViewController] bind:@"representedObject" toObject:self withKeyPath:@"selectedName" options:nil];
}
- (RightViewController *)rightSplitViewController
{
if (!_rightSplitViewController) {
NSArray<NSViewController *>*vcs = [[[self window] contentViewController] childViewControllers];
for (NSViewController *vc in vcs) {
if ([vc isKindOfClass:[RightViewController class]]) {
_rightSplitViewController = (RightViewController *)vc;
break;
}
}
}
return _rightSplitViewController;
}
- (LeftViewController *)leftSplitViewController
{
if (!_leftSplitViewController) {
NSArray<NSViewController *>*vcs = [[[self window] contentViewController] childViewControllers];
for (NSViewController *vc in vcs) {
if ([vc isKindOfClass:[LeftViewController class]]) {
_leftSplitViewController = (LeftViewController *)vc;
break;
}
}
}
return _leftSplitViewController;
}
- (NSWindow *)window
{
return [[self windowController] window];
}
//VALID SOLUTION IF YOU DON'T INSTANTIATE STORYBOARD
//- (NSWindow *)window
//{
// return [[NSApp windows] firstObject];
//}
@end
结果:工作完全相同
PS:如果您实例化自己的窗口控制器,请不要忘记从 Storyboard 中删除初始控制器
为什么这样更好?导致所有更改都转到模型,模型发送触发器以重绘视图。此外,您最终会使用更小的视图控制器。
还能做些什么?NSObjectController
是模型对象和视图之间最好的粘合剂。它还可以防止绑定有时可能发生的保留周期(更高级的主题)。NSArrayController
等等...
警告:不是 XIB 的解决方案