【发布时间】:2017-08-13 09:42:29
【问题描述】:
我正在尝试更新连接到 myViewController 的 NSTextView 的内容,作为文件所有者的引用出口,该文件所有者是 myViewController 的子类。
当我从按钮使用 IBAction 或使用控制器的 viewDidLoad 方法时,我可以很好地更新文本。但是,当我尝试从另一个类(在此示例中称为anotherViewController)运行该方法时,它会运行该方法,但 textview 不会更改。
myViewController.h:
#import <Cocoa/Cocoa.h>
#import "anotherViewController.h"
@interface myViewController : NSViewController { }
@property (unsafe_unretained) IBOutlet NSTextView *outText;
@property (weak) IBOutlet NSButton *updateMeButton;
- (void)updateTextView:(NSString *)argText;
- (void)updateTextViewWithoutArg;
@end
myViewController.m:
#import "myViewController.h"
@interface myViewController ()
@end
@implementation myViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.outText.string = @"I work successfully";
}
- (IBAction)updateMeButton:(id)sender {
self.outText.string = @"I am updated text! I also work!";
}
- (void)updateTextView:(NSString *)argText {
self.outText.string = @"I don't make it to the NSTextView :(";
NSLog(@"Should have updated text view");
}
- (void)updateTextViewWithoutArg {
self.outText.string = @"I don't make it to the NSTextView :(";
NSLog(@"Should have updated text view");
}
@end
在具有所有相关导入的 anotherViewController.m 中,我称之为:
myViewController *viewtask = [[myViewController alloc] init];
[viewtask updateTextViewWithoutArg];
什么都没有发生。该方法运行并记录它应该已更新,但没有文本更新。我尝试了许多不同的方法,包括 textstorage 和 scrollrange 方法,它们都可以在已经工作的部分工作,但在不工作的部分没有区别。
我也尝试过只是为了好玩:
myViewController *viewtask;
[viewtask updateTextViewWithoutArg];
- 同样使用实例变量
_outText - 也使用
[self.outText setString:@"string"]; - 也使用
[_outText setString:@"string"];
同样,它们可以工作,但仅限于已经工作的部分。
这应该很简单,但对我来说不合逻辑。我需要做的就是迅速
self.outText.string = "I update whenever I'm called!"
【问题讨论】:
-
您必须将
updateTextViewWithoutArg发送到现有的视图控制器。[[myViewController alloc] init]创建一个新的视图控制器。 -
我没有投反对票。
标签: objective-c