设置 viewController 共享 public 对象
Set viewController shared public object
我有 ViewController
,其中包含 tableView
和要在 table 上显示的项目数组。
为了更改 table 内容,我可以初始化数组并从 ViewController 按钮回调中调用 reloadData 方法(参见下面的 buttonTap
方法)。
然而,我还需要从 viewController
范围之外的外部代码更改 table 内容,但无法访问。
在下面的最小示例中,我使用了基于外部 C/C++ 的线程,该线程尝试访问 viewController
方法失败但由于 No known class method for selector 'changeArr'
[= 而在编译时失败20=]
知道如何制作 viewController public 吗?
viewController.m
#import "ViewController.h"
@interface ViewController() <NSTableViewDelegate, NSTableViewDataSource>
@property (weak) IBOutlet NSTableView *tableView;
@property NSMutableArray<NSString*> * arr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];
}
- (void)viewWillAppear {
[super viewWillAppear];
[self.tableView reloadData];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return self.arr.count;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSTableCellView* cellView =(NSTableCellView*)[tableView makeViewWithIdentifier:@"cell1" owner:nil];
cellView.textField.stringValue = self.arr[row];
return cellView;
}
-(void)changeArr {
self.arr = [NSMutableArray arrayWithObjects:@"ccc", @"ddd", nil];
[self.tableView reloadData];
NSLog(@"fwefwe");
}
- (IBAction)buttonTap:(id)sender {
self.arr = [NSMutableArray arrayWithObjects:@"AAAA", @"BBB", nil];
[self.tableView reloadData];
}
main.m
#import <Cocoa/Cocoa.h>
#include <pthread.h>
#import "ViewController.h"
void updateTable() {
sleep(10);
dispatch_sync(dispatch_get_main_queue(), ^{ [ViewController changeArr]; }); // ERROR : No known class method for selector 'changeArr'
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Setup code that might create autoreleased objects goes here.
}
pthread_t threadHandler;
pthread_create(&threadHandler, NULL, &updateTable, NULL);
return NSApplicationMain(argc, argv);
}
changeArr
是私有函数(-)。所以你不能调用[ViewController changeArr]
。
如果将 changeArr
更改为 public fuction(+),则无法访问内部数组。
所以我认为完成这个任务的简单方法是通过单例使用 ViewController
的实例。
...
@implementation ViewController
+ (id) sharedViewController {
static ViewController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
sharedInstance = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"];
});
return sharedInstance ;
}
- (void)viewDidLoad {
[super viewDidLoad];
...
并在 updateTable 中:
dispatch_sync(dispatch_get_main_queue(), ^{ [[ViewController sharedViewController] changeArr]; });
我有 ViewController
,其中包含 tableView
和要在 table 上显示的项目数组。
为了更改 table 内容,我可以初始化数组并从 ViewController 按钮回调中调用 reloadData 方法(参见下面的 buttonTap
方法)。
然而,我还需要从 viewController
范围之外的外部代码更改 table 内容,但无法访问。
在下面的最小示例中,我使用了基于外部 C/C++ 的线程,该线程尝试访问 viewController
方法失败但由于 No known class method for selector 'changeArr'
[= 而在编译时失败20=]
知道如何制作 viewController public 吗?
viewController.m
#import "ViewController.h"
@interface ViewController() <NSTableViewDelegate, NSTableViewDataSource>
@property (weak) IBOutlet NSTableView *tableView;
@property NSMutableArray<NSString*> * arr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];
}
- (void)viewWillAppear {
[super viewWillAppear];
[self.tableView reloadData];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return self.arr.count;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSTableCellView* cellView =(NSTableCellView*)[tableView makeViewWithIdentifier:@"cell1" owner:nil];
cellView.textField.stringValue = self.arr[row];
return cellView;
}
-(void)changeArr {
self.arr = [NSMutableArray arrayWithObjects:@"ccc", @"ddd", nil];
[self.tableView reloadData];
NSLog(@"fwefwe");
}
- (IBAction)buttonTap:(id)sender {
self.arr = [NSMutableArray arrayWithObjects:@"AAAA", @"BBB", nil];
[self.tableView reloadData];
}
main.m
#import <Cocoa/Cocoa.h>
#include <pthread.h>
#import "ViewController.h"
void updateTable() {
sleep(10);
dispatch_sync(dispatch_get_main_queue(), ^{ [ViewController changeArr]; }); // ERROR : No known class method for selector 'changeArr'
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Setup code that might create autoreleased objects goes here.
}
pthread_t threadHandler;
pthread_create(&threadHandler, NULL, &updateTable, NULL);
return NSApplicationMain(argc, argv);
}
changeArr
是私有函数(-)。所以你不能调用[ViewController changeArr]
。
如果将 changeArr
更改为 public fuction(+),则无法访问内部数组。
所以我认为完成这个任务的简单方法是通过单例使用 ViewController
的实例。
...
@implementation ViewController
+ (id) sharedViewController {
static ViewController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
sharedInstance = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"];
});
return sharedInstance ;
}
- (void)viewDidLoad {
[super viewDidLoad];
...
并在 updateTable 中:
dispatch_sync(dispatch_get_main_queue(), ^{ [[ViewController sharedViewController] changeArr]; });