gpt4 book ai didi

objective-c - KVO方法observeValueForKeyPath通知我有关更改的信息,但我无法在viewDidLoad中使用它

转载 作者:行者123 更新时间:2023-12-03 17:38:23 24 4
gpt4 key购买 nike

这几天我正在工作,我不知道我做错了什么。

首先,我不知道 KVO 是否是将单击的行信息的值从一个 View Controller 传递到另一个 View Controller 的最佳方式。

我有主视图,只有 2 个按钮。单击其中一个按钮,我打开 MasterTableViewController,其中有 NSTableView 以及来自核心数据的一些记录(仅一列)。

单击 NSTableView 时,我将打开 DetailViewController,其中只有一个标签。

我正在尝试更改标签值以及 NSTableView 中单击行的值。

虽然我可以打印 DetailViewController 中单击行的值,但无法更改标签值。我假设通知在 viewDidLoad 被调用之前发出。

我有以下代码:

在我的MasterTableViewController.h文件中:

#import <Cocoa/Cocoa.h>
#import "AppDelegate.h"

@interface MasterTableViewController : NSViewController

@property (nonatomic,strong) NSManagedObjectContext *mObjContext;
@property (weak) IBOutlet NSTableView *websitesTableView;
- (IBAction)tableViewDoubleClick:(id)sender;

@property (nonatomic,strong) NSString *cellValue;
@end

在我的MasterTableViewController.m文件中:

#import "MasterTableViewController.h"
#import "DetailViewController.h"
@interface MasterTableViewController ()

@end

@implementation MasterTableViewController

-(void)awakeFromNib {
[_websitesTableView setTarget:self];
[_websitesTableView setDoubleAction:@selector(tableViewDoubleClick:)];
}



- (void)viewDidLoad {
[super viewDidLoad];
// Get the object managed context
AppDelegate *appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];
self.mObjContext = appDelegate.managedObjectContext;

}


- (IBAction)tableViewDoubleClick:(id)sender {
NSInteger rowNumber = [_websitesTableView clickedRow];
NSTableColumn *column = [_websitesTableView tableColumnWithIdentifier:@"websiteUrl"];
NSCell *cell = [column dataCellForRow:rowNumber];

_cellValue = [cell stringValue];

MasterTableViewController *mtvc = [[MasterTableViewController alloc]initWithNibName:@"MasterTableViewController" bundle:nil];
DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];


[mtvc addObserver:dvc
forKeyPath:@"cellValue"
options:NSKeyValueObservingOptionNew
context:NULL];

[mtvc setCellValue:_cellValue];
[mtvc removeObserver:dvc forKeyPath:@"cellValue"];


AppDelegate *appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];
[appDelegate changeViewController:2];
}

@结束

DetailViewController.h 的代码是:

#import <Cocoa/Cocoa.h>
#import "AppDelegate.h"
@interface DetailViewController : NSViewController
@property (nonatomic, weak) AppDelegate *appDelegate;
@property (weak) IBOutlet NSTextField *detailLabel;
@property (nonatomic,strong) NSString *transferedLabelValue;
@property (nonatomic,strong) NSString *cellValue;

@end

DetailViewController.m 的代码是:

#import "DetailViewController.h"
#import "MasterTableViewController.h"
@interface DetailViewController ()

@end

@implementation DetailViewController

- (void)viewDidLoad {
[super viewDidLoad];
// if I uncomment this line, program crashes, because the value of the _transferedLabelValue is null here.
//[_detailLabel setStringValue:_transferedLabelValue];

}

-(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqualToString:@"cellValue"]) {

_transferedLabelValue = [change objectForKey:NSKeyValueChangeNewKey];
// Here I get the value of the clicked cell, it is printed in the console. However if I try to change the label value here, doesn't work, since seems that label does not exist yet at this point....
NSLog(@"Value in the observerValueForKeyPath is:%@",_transferedLabelValue);
}

}

@结束

AppDelegate.h 中的代码:

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@property (weak) IBOutlet NSView *mainAppView;
@property (nonatomic,strong) NSViewController *mainAppViewController;

- (IBAction)showMasterViewButtonHasBeenClicked:(id)sender;
- (IBAction)showDetailViewButtonHasBeenClicked:(id)sender;
- (void)changeViewController:(NSInteger)tag;

@end

这里仅是AppDelegatge.m中的相关代码,省略了样板代码。

#import "AppDelegate.h"
#import "MasterTableViewController.h"
#import "DetailViewController.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
- (IBAction)saveAction:(id)sender;

@end

@implementation AppDelegate

- (IBAction)showMasterViewButtonHasBeenClicked:(id)sender {
NSInteger tag = [sender tag];
[self changeViewController:tag];
}

- (IBAction)showDetailViewButtonHasBeenClicked:(id)sender {
NSInteger tag = [sender tag];
[self changeViewController:tag];
}

- (void)changeViewController:(NSInteger)tag {
[[_mainAppViewController view]removeFromSuperview];

switch (tag) {
case 1:

self.mainAppViewController = [[MasterTableViewController alloc]initWithNibName:@"MasterTableViewController" bundle:nil];
[_mainAppView addSubview:[_mainAppViewController view]];
[[_mainAppViewController view] setFrame:[_mainAppView bounds]];
[[_mainAppViewController view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
break;

case 2:

self.mainAppViewController = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
[_mainAppView addSubview:[_mainAppViewController view]];
[[_mainAppViewController view] setFrame:[_mainAppView bounds]];
[[_mainAppViewController view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

break;

}

}

再次,如果 KVO 和通知不是最好的方法,请让我知道我应该如何“通知”另一个 View 哪一行已被单击以及他的值是多少。

问候,约翰

最佳答案

我设法解决了 KVO 的问题...我的问题是我在 MasterTableViewController 中创建 View Controller 的新实例...我所做的是在 appDelegatge.h 中为 ViewController 创建属性,例如这个:

@property MasterTableViewController *mtvc;
@property DetailViewController *dvc;

然后在 appDelegate.m 中:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
_mtvc = [[MasterTableViewController alloc]initWithNibName:@"MasterTableViewController" bundle:nil];
_dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
}

代码中的其余更改只是将 MasterTableViewController 和 DetailViewController 替换为 _mtvc 和 _dvc...

但是...我被警告说这个解决方案容易出错,并且该警告显示为真。有时我会得到错误的值,而不是单击的值。一旦点击 4-5 次,我就会得到错误的值。让我感到困惑的是,我让该函数在双击时工作,但它也可以在单击时工作......但我猜这些东西是为了解决其他问题。

关于objective-c - KVO方法observeValueForKeyPath通知我有关更改的信息,但我无法在viewDidLoad中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32151968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com