从另一个类调用时 NSTextfield 未更新
我有一个 runthis.h 文件,其中有一个标签:
IBOutlet NSTextField *updateStatus;
使用 Now 来更新它
-(IBAction) startTest:(id)sender {
[updateStatus setStringValue:@"Testing"];
}
现在在我的 runthis.m 文件中,在一个名为 startTest 的类中,如果我在另一个文件testing.m 中为 runthis 类创建一个对象,然后尝试 :
runthis *testSomething = [[runthis alloc] init];
[testSomething performSelectorInBackground:@selector(startTest:) withObject:nil];
但我发现当我从testing.m调用它时,标签*updateStatus的UI永远不会设置为“Testing” 如果我直接从 runthis.m 调用它,那么 UI 将按预期更新。有什么想法吗?谢谢。
I have a runthis.h file where I have a label:
IBOutlet NSTextField *updateStatus;
Now in my runthis.m file, in a class called startTest I update this using
-(IBAction) startTest:(id)sender {
[updateStatus setStringValue:@"Testing"];
}
Now if I create an object for the runthis class in another file testing.m and then try this:
runthis *testSomething = [[runthis alloc] init];
[testSomething performSelectorInBackground:@selector(startTest:) withObject:nil];
But I find the UI for the label *updateStatus will never get set to "Testing" when I call it from testing.m
If I call this directly from runthis.m, then the UI gets updated as expected. Any ideas why ? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非
runthis
位于单独的笔尖中,否则它的插座不会自动连接,您必须将其作为引用传递。在testing
中,您必须连接文本字段,然后将其传递给runthis
,如下所示:或者您可以创建对
testing
的引用来自您的runthis
对象(将其称为委托)并让testing
直接更新 UI。这就是我要使用的方法。Unless
runthis
is located in a separate nib, it's outlet won't be connected automatically, you'll have to pass it as a reference. Intesting
you'll have to wire up the text field, then pass it torunthis
like:Or you could create a reference to your
testing
object from yourrunthis
object (call it a delegate) and havetesting
update the UI directly. That's the approach I would use.